Virtualenv: site.getusersitepackages() function missing from virtualenv embedded site.py

Created on 2 Oct 2015  路  10Comments  路  Source: pypa/virtualenv

Python 2.7 has a builtin function called getusersitepackages() inside the 'site' module:
https://docs.python.org/2/library/site.html#site.getusersitepackages

However, it seems that when one creates a virtualenv, an embedded version of this site.py file is installed in the environment:
https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/site.py
This version doesn't have getusersitepackages().

These causes scripts that rely on this functionality to fail.
(More specifically, the installation of e.g. this package using Homebrew for Mac:
https://github.com/Homebrew/homebrew-science/blob/master/neuron.rb)

Most helpful comment

I found thonny installed in a virtualenv didn't work and went hunting to land on this issue. It seems you can just add the functions copied from the standard distribution into the virtualenv's site.py for a workaround. At least that has worked for my case.

--- py36/lib/python3.6/site.py  2017-08-17 08:14:16.364110854 -0600
+++ thonny/lib/python3.6/site.py    2017-08-16 13:18:24.240013957 -0600
@@ -306,6 +306,43 @@

     return True

+def getuserbase():
+    """Returns the `user base` directory path.
+
+    The `user base` directory can be used to store data. If the global
+    variable ``USER_BASE`` is not initialized yet, this function will also set
+    it.
+    """
+    global USER_BASE
+    if USER_BASE is not None:
+        return USER_BASE
+    from sysconfig import get_config_var
+    USER_BASE = get_config_var('userbase')
+    return USER_BASE
+
+def getusersitepackages():
+    """Returns the user-specific site-packages directory path.
+
+    If the global variable ``USER_SITE`` is not initialized yet, this
+    function will also set it.
+    """
+    global USER_SITE
+    user_base = getuserbase() # this will also set USER_BASE
+
+    if USER_SITE is not None:
+        return USER_SITE
+
+    from sysconfig import get_path
+
+    if sys.platform == 'darwin':
+        from sysconfig import get_config_var
+        if get_config_var('PYTHONFRAMEWORK'):
+            USER_SITE = get_path('purelib', 'osx_framework_user')
+            return USER_SITE
+
+    USER_SITE = get_path('purelib', '%s_user' % os.name)
+    return USER_SITE
+
 def addusersitepackages(known_paths):
     """Add a per user site-package to sys.path

All 10 comments

This also seems to affect installing llvm using Homebrew for Mac Yosemite:
https://github.com/Homebrew/homebrew/blob/master/Library/Formula/llvm.rb

The errors:

==> Downloading https://homebrew.bintray.com/bottles/llvm-3.6.2.yosemite.bottle.1.tar.gz

################################################################## 100.0%

==> Pouring llvm-3.6.2.yosemite.bottle.1.tar.gz
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'getusersitepackages'
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'getusersitepackages'

This also affects other brew installed packages. My problem was with gdal.

We are also having this issue which restricts us to use virtualenv. Any milestones ?

I've had this problem installing Postgis through brew

$ brew install postgis
==> Installing dependencies for postgis: proj, geos, json-c, libpng, jpeg, giflib, libtiff, lzlib, libgeotiff, freexl, libxml2, liblwgeom, libsp
==> Installing postgis dependency: proj
==> Downloading https://homebrew.bintray.com/bottles/proj-4.9.2.el_capitan.bottle.tar.gz
######################################################################## 100,0%
==> Pouring proj-4.9.2.el_capitan.bottle.tar.gz
馃嵑  /usr/local/Cellar/proj/4.9.2: 52 files, 5.9M
==> Installing postgis dependency: geos
==> Downloading https://homebrew.bintray.com/bottles/geos-3.5.0.el_capitan.bottle.1.tar.gz
######################################################################## 100,0%
==> Pouring geos-3.5.0.el_capitan.bottle.1.tar.gz
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getusersitepackages'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getusersitepackages'

I have this problem too; Is there a workaround?

Also faced the same issue....Anyone has good suggestions?

==> Pouring wxpython-3.0.2.0.el_capitan.bottle.1.tar.gz Traceback (most recent call last): File "<string>", line 1, in <module> AttributeError: 'module' object has no attribute 'getusersitepackages' Traceback (most recent call last): File "<string>", line 1, in <module> AttributeError: 'module' object has no attribute 'getusersitepackages' ==> Caveats Python modules have been installed and Homebrew's site-packages is not in your Python sys.path, so you will not be able to import the modules this formula installed. If you plan to develop with these modules, please run: mkdir -p echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> homebrew.pth

More likely than not, its related to a native .dylib that is compiled for wrong architecture.

Try running
brew install -vd <package>
To confirm if it breaks at the point of .dylib being ID'd. If yes, you might want to try reinstall from source:

brew install -vd --build-from-source <package>

If that doesn't work either, there may be a problem with how source package builds dylibs. In my case, SWIG script was simply broken , determined with

otool -L /usr/local/.../broken.dylib

Or, it might be something completely unrelated. -vd should help trace it down though. Can also happen if python paths are strange in the current shell, or you are running brew from virtualenv

I found thonny installed in a virtualenv didn't work and went hunting to land on this issue. It seems you can just add the functions copied from the standard distribution into the virtualenv's site.py for a workaround. At least that has worked for my case.

--- py36/lib/python3.6/site.py  2017-08-17 08:14:16.364110854 -0600
+++ thonny/lib/python3.6/site.py    2017-08-16 13:18:24.240013957 -0600
@@ -306,6 +306,43 @@

     return True

+def getuserbase():
+    """Returns the `user base` directory path.
+
+    The `user base` directory can be used to store data. If the global
+    variable ``USER_BASE`` is not initialized yet, this function will also set
+    it.
+    """
+    global USER_BASE
+    if USER_BASE is not None:
+        return USER_BASE
+    from sysconfig import get_config_var
+    USER_BASE = get_config_var('userbase')
+    return USER_BASE
+
+def getusersitepackages():
+    """Returns the user-specific site-packages directory path.
+
+    If the global variable ``USER_SITE`` is not initialized yet, this
+    function will also set it.
+    """
+    global USER_SITE
+    user_base = getuserbase() # this will also set USER_BASE
+
+    if USER_SITE is not None:
+        return USER_SITE
+
+    from sysconfig import get_path
+
+    if sys.platform == 'darwin':
+        from sysconfig import get_config_var
+        if get_config_var('PYTHONFRAMEWORK'):
+            USER_SITE = get_path('purelib', 'osx_framework_user')
+            return USER_SITE
+
+    USER_SITE = get_path('purelib', '%s_user' % os.name)
+    return USER_SITE
+
 def addusersitepackages(known_paths):
     """Add a per user site-package to sys.path

This is an old thread but still comes up in Google results so I am leaving this tip here:

  • ALWAYS deactivate virtualenv before using brew

(That's what I have learned to do)

  • For other cases (if for some reason you _need_ to have virtualenv activated at the same time), try @mwichmann 's tip ;)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manthey picture manthey  路  4Comments

mitchhentges picture mitchhentges  路  3Comments

erbatyr picture erbatyr  路  5Comments

schlamar picture schlamar  路  4Comments

neildhar picture neildhar  路  4Comments