Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cocoa-maemae opened this issue Jul 4, 2019 · 10 comments
Closed

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

cocoa-maemae opened this issue Jul 4, 2019 · 10 comments

Comments

@cocoa-maemae
Copy link

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()"

@frostming
Copy link
Contributor

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

Close it for now

@vashek
Copy link

vashek commented Nov 11, 2019

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

@lfsando
Copy link

lfsando commented Nov 11, 2019

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'

@vashek
Copy link

vashek commented Nov 11, 2019

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.

@maludwig
Copy link

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

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.

@jueti
Copy link

jueti commented Nov 12, 2019

work for me
thanks 🍺

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

@fabiencelier
Copy link

fabiencelier commented Nov 12, 2019

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.

@jemre
Copy link

jemre commented Nov 12, 2019

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

@S4lem
Copy link

S4lem commented Aug 3, 2023

I received this same error message with pyInstaller package. The reason is because I was from inside a virtualenv.
I fixed it by downgrading pyinstaller==5.0.1

@nickolay
Copy link

nickolay commented Nov 2, 2023

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

For those (like me) living under a rock: This was fixed in virtualenv 20.0+, and upgrading to pipenv 2023.2.18+ will pull the fixed virtualenv version. You then have to re-create the pipenv environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants