python - completely self-contained virtual environment -
i create python3
virtual environment (explicitly avoiding symlinks, --copies
):
» python3 -m venv --without-pip --copies venv
this complete virtual environment now:
» tree venv/ venv/ ├── bin │ ├── activate │ ├── activate.csh │ ├── activate.fish │ ├── python │ └── python3 ├── include ├── lib │ └── python3.4 │ └── site-packages ├── lib64 -> lib └── pyvenv.cfg
i disable pythonpath
, make sure nothing leaking outside:
» pythonpath=""
activate venv:
» source venv/bin/activate
verify activate
has not polluted pythonpath
:
» echo $pythonpath
(blank, expected)
i using right python:
» python /foo/bar/venv/bin/python
but system modules still being accessed:
» python python 3.4.3 (default, oct 14 2015, 20:28:29) [gcc 4.8.4] on linux type "help", "copyright", "credits" or "license" more information. >>> import unittest >>> print(unittest) <module 'unittest' '/usr/lib/python3.4/unittest/__init__.py'> >>>
i expect import unittest
statement fail, since virtual environment has no such module.
i know:
- why system packages accessed when in virtualenv?
- how can create self-contained virtual environment?
if recall correctly core system packages symlinked, same files (partly keep size of virtualenv down).
the default not include site-packages
directory, won't access 3rd party libraries have been installed.
if want isolated , self-contained virtual environment, might better off looking @ docker.
virtualenv more of lightweight way of managing different 3rd party installed packages different apps.
edit:
it looks --always-copy
doesn't copy files:
virtualenv doesn't copy .py files lib/python directory
digging source , looks there's smallish set of modules deemed "required" , these ones copied:
https://github.com/pypa/virtualenv/blob/ac4ea65b14270caeac56b1e1e64c56928037ebe2/virtualenv.py#l116
edit 2:
you can see old python directories still appear in sys.path
, after directories virtualenv itself:
>>> import sys >>> sys.path ['', '/home/john/venv/lib/python2.7', '/home/john/venv/lib/python2.7/plat-linux2', '/home/john/venv/lib/python2.7/lib-tk', '/home/john/venv/lib/python2.7/lib-old', '/home/john/venv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/home/john/venv/local/lib/python2.7/site-packages', '/home/john/venv/lib/python2.7/site-packages']
Comments
Post a Comment