130

I have created a virtualenv using the --no-site-packages option and installed lots of libraries. Now I would like to revert the --no-site-packages option and use also the global packages.

Can I do that without recreating the virtualenv?

More precisely:

I wonder what exactly happens when creating a virtualenv using the --no-site-packages option as opposed to not using that option.

If I know what happens then I can figure out how to undo it.

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
  • [an other question](http://stackoverflow.com/questions/12433198/) was marked as duplicate of this one, and it received an answer which might be interesting here too. – mariotomo Jan 23 '14 at 13:39

6 Answers6

164

Try removing (or renaming) the file no-global-site-packages.txt in your Lib folder under your virtual environment.

Where venv is the name of your virtual environment, and python3.4 corresponds to whichever version of python involved, for example:

$ rm venv/lib/python3.4/no-global-site-packages.txt

And if you change your mind and want to put it back:

$ touch venv/lib/python3.4/no-global-site-packages.txt

Note: If you don't see the above file, then you have a newer version of virtualenv. You'll want to follow this answer instead

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
ars
  • 120,335
  • 23
  • 147
  • 134
  • 18
    That will do it. The existence (or nonexistence) of that file is the only direct effect of the no-site-packages flag. Virtualenv's customized site.py looks for that file to decide whether to add global site-packages directories to sys.path. – Carl Meyer Jul 31 '10 at 02:58
  • Brilliant! That did it! Thanks a lot for that answer, and thanks to Carl Meyer for the additional comment. – Olivier Verdier Jul 31 '10 at 08:44
  • 1
    that's `virtualenv/python2.7/no-global-site-packages.txt` in my ubuntu server. – caesarsol Jan 29 '14 at 10:53
  • 5
    Windows version of virtualenv contains `virtualenv/pyenv.cfg` file. There is one option for site-packages: `include-system-site-packages = false` Change this value and call Activate.ps1 – georgik May 24 '15 at 12:12
  • @georgik - that also works for Linux version of `venv` for `Python 3.4`. – Tomasz Dzieniak Apr 21 '16 at 06:37
  • This does not work with Anaconda virtual environments. – Shailen Dec 01 '18 at 01:47
  • 2
    This file doesn't seem to appear anymore. See https://stackoverflow.com/a/58513801/21539 for a updated answer – Zain Rizvi Oct 23 '19 at 00:11
  • Obsolete answer – BjornW Mar 23 '23 at 14:14
30

At least for Python 3.5.2, there is pyvenv.cfg file in the root of virtualenv directory. All you need to do is to change include-system-site-packages flag from false to true:

home = /usr/bin
include-system-site-packages = false  # <- change this to "true"
version = 3.5.2
kotrfa
  • 1,191
  • 15
  • 22
  • This helped me a lot, I was going mad trying to do the reverse (get a venv to let programs use the global packages) and it seems the venv is created by default with this set to false – BjornW Mar 23 '23 at 14:14
20

Go to your venv folder and open pyvenv.cfg. (E.g. if your virtual environment is called myenv then the file will be located at myenv\pyvenv.cfg)

You'll see a boolean setting called include-system-site-packages

Set that setting to true to use global packages

If you want to disable using global packages, just set that setting to false instead.

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
  • Upvoting this for higher visibility. Should be accepted answer, because most users have latest venv running. – Murtaza Haji Mar 01 '21 at 15:20
  • This is essentially the same as the three-years-earlier [answer by kotrfa](https://stackoverflow.com/a/40972692/95852). – John Y Jan 04 '23 at 15:27
12

When using virtualenvwrapper to manage virtualenvs, you can use the shell function toggleglobalsitepackages to switch between using and not using site packages.

Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • This only works with older versions (not sure when this changed), when virtualenv still used the existance of `no-global-site-packages.txt` as the flag. Newer versions use a setting in the file pyvenv.cfg instead. It seems that virtualenv_wrapper has not been updated to work with this change to virtualenv https://bitbucket.org/virtualenvwrapper/virtualenvwrapper/issues/345/toggleglobalsitepackages-command-seems. – Mads Skjern Jun 23 '22 at 17:51
2

Try adding a symlink between /virtualenv_root/lib/ and /path/to/desired/site-packages/

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • +1 This works on my system (openSUSE Linux 12.3, Python 2.7.3, virtualenv 1.8.4); I don't have a `no-global-site-packages.txt` anywhere. – Aaron Digulla Dec 04 '13 at 17:46
-1

Where 'myvenv' is the name of your virtual environment, and python3.8, like mine, for example, all you need to do in command line is:

$ python3 -m venv --system-site-packages myvenv
alexzorgo
  • 1
  • 1