129

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
  • 44,254
  • 26
  • 97
  • 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
  • 22,693
  • 19
  • 86
  • 125
ars
  • 113,841
  • 22
  • 139
  • 133
  • 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
  • 1
    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
27

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,121
  • 13
  • 21
15

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
  • 22,693
  • 19
  • 86
  • 125
12

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

Adaephon
  • 14,909
  • 1
  • 46
  • 66
2

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

Tim McNamara
  • 17,513
  • 4
  • 50
  • 80
  • +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