Virtualenv in Python Flashcards
What is virtualenv
What is:
virtualenv is a tool to create isolated Python environments, it is the
easest way to configure a custom Python environment
It allows you to add and modify Python modules without access to the
global installation
What does virtualenv do
It addresses one problem of dependencies and versions and indirectly permissions. Practically, we all face these problems:
- Looking for a way to configure different versions of libraries to
different application. It is not advisable to install each version in
to you library. For an application, any small changes in its
supportive library will break the application
- We cannot install the desired library into the site-packages path
without root.
Virtualenv can solve such problems. Virtualenv has a favorable feature that it creates an enviroment that has its own installation directories which doesn’t share libraries with other virtualenv environments. Namely, connect corresponding environment to corresponding application
virtualenv directory-name –no-site-packages
what is the option –no-site-packages in this expression
–no-site-packages option will tell virtualenv that the sys.path will not include site packages but everything else will remain there
How to use virtualenv
run virtualenv env_name
env_name/lib/ and env_name/include/ are created, anything
installed will be under env_name/lib/pythonX.X/site-packages
add #!/path/to/env_name/bin/python to the scripts
pip is also installed isolatedly
Note: many things in this isolated environment are just links to the systemwide python.
run source bin/activate
This will change $PATH so its first entry is the virtualenv’s bin/
If you run python in the env_name/bin/python, you dont have to use
source command
To undo the PATH changes, you just run deactivate
The –system-site-packages option
–system-site-packages will make your lib inherit the systemwide site-packages. If you want an isolated environment, you should not use the option.