Pipenv: AttributeError: module 'site' has no attribute 'getsitepackages'

Created on 4 Jul 2019  路  8Comments  路  Source: pypa/pipenv

Issue description

python version 3.7.3
pipenv version 2018.11.26

When I try to use site.py#getsitepackages() in pipenv environment, the below error occurrs.

AttributeError: module 'site' has no attribute 'getsitepackages'

When I use the same module in pip environment, the error does not occur.

The cause is probably that pipenv doesn't refer site.py installed in python environment.
I checked the installed directory.

pipenv --venv
$HOME/.local/share/virtualenvs/$hash
ls -l $HOME/.local/share/virtualenvs/$hash/lib/python3.7 | grep site.py
-rw-rw-r--   1 $user $user 28389 Jun  6 07:13 site.py

The aforementioned site.py does not have getsitepackages() method.

The other modules refer to the modules installed in python environment.

E.g.
os.py

ls -l $HOME/.local/share/virtualenvs/$hash/lib/python3.7 | grep os.py
lrwxrwxrwx   1 $user $user    51 Jun  6 07:13 os.py -> /usr/local/pyenv/versions/3.7.3/lib/python3.7/os.py

Expected result

Even in pipenv environment, site.py#getsitepackages() method is available.

Steps to replicate

Install python3.7.3
Install pipenv by pip
Run the below
pipenv run python -c "import site; site.getsitepackages()"

Most helpful comment

I've gotten this issue on Python 3.7.0, Windows 10, in a virtualenv.

As a workaround that seems to be holding, outside of the virtualenv, I did:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()
['C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

Then I simply edited the site.py in my virtualenv, and added the missing function:

def getsitepackages():
    return ['C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

It's a hacky workaround, but hopefully this helps someone else.

All 8 comments

It is an issue with virtualenv, which copies the bundled version of site.py to the venv when it is created. (https://github.com/pypa/virtualenv/issues/737)

Close it for now

This now breaks pywin32 installation, starting from pywin32==226. :(
Because of this: https://github.com/mhammond/pywin32/commit/71afa71e11e6631be611ca5cb57cda526b5e91ab#diff-1fbc440d4725e98acddc7667ffb63fe6
Not specific to pipenv. Relevant issue in pywin32: https://github.com/mhammond/pywin32/issues/1439

This started happening today.
I installed a package in pipenv and started throwing this error.
I then deleted the environment and created a new, now it won't work.
I can't even open python.exe

File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site.py", line 791, in <module> main() File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site.py", line 768, in main paths_in_sys = addsitepackages(paths_in_sys) File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site.py", line 280, in addsitepackages addsitedir(sitedir, known_paths) File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site.py", line 211, in addsitedir addpackage(sitedir, name, known_paths) File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site.py", line 179, in addpackage exec(line) File "<string>", line 1, in <module> File "C:\Users\--\.virtualenvs\project-nNknfD7x\lib\site-packages\win32\lib\pywin32_bootstrap.py", line 14, in <module> for maybe in site.getsitepackages(): AttributeError: module 'site' has no attribute 'getsitepackages'

This started happening today.
I installed a package in pipenv and started throwing this error.
I then deleted the environment and created a new, now it won't work.
I can't even open python.exe

Delete the virtual environment, change the pywin32 environment in your Pipfile to "==225", and then run pipenv update.
If you don't have a pywin32 requirement, it's probably required by another required package. Just add pywin32 with the "==225" version into your Pipfile.

I've gotten this issue on Python 3.7.0, Windows 10, in a virtualenv.

As a workaround that seems to be holding, outside of the virtualenv, I did:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getsitepackages()
['C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

Then I simply edited the site.py in my virtualenv, and added the missing function:

def getsitepackages():
    return ['C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\maludwig\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

It's a hacky workaround, but hopefully this helps someone else.

work for me
thanks :beer:

def getsitepackages():
    return ['C:\\Users\\***\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\***\\.virtualenvs\\Wolfram-OchuDyJ5\\Lib\\site-packages']

Happened to me on my windows CI too :

Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site.py", line 769, in 
    main()
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site.py", line 746, in main
    paths_in_sys = addsitepackages(paths_in_sys)
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site.py", line 279, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site.py", line 202, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site.py", line 170, in addpackage
    exec(line)
  File "", line 1, in 
  File "c:\\users\\circleci\\.virtualenvs\\project-ohnhamvy\\lib\\site-packages\\win32\\lib\\pywin32_bootstrap.py", line 14, in 
    for maybe in site.getsitepackages():', "AttributeError: module 'site' has no attribute 'getsitepackages'"

We don't have pywin32 as a direct dependency and I don't think pywin32==225 is viable long term solution.

Thanks @maludwig. Your work around worked well. Hope there's a long term solution soon cause I have a lot of different virtual environments. 馃槶

Was this page helpful?
0 / 5 - 0 ratings