Tags: #python3
Use virtualenv to allow each of your projects to have their own set of dependencies. This is similar to how Ruby has Bundler and Gemfiles so you don’t install certain packages globally.
sudo pip install virtualenv
(installs it globally; ironic I know but this is OK)virtualenv env
(env
is the dir that’ll be created; this name is common practice)Avoid commiting env
into version control by utilising .gitignore
virtualenv .
side-steps the folder creation if you really prefer
Now you can install packages using:
env/bin/pip install <package>
To run the Python binary you don’t use the system version; instead:
env/bin/python <...>
If you don’t like typing, you can shorten the env/bin
so it becomes part of your shell’s environment:
source env/bin/activate
To demonstrate:
which python
= /usr/bin/python
source env/bin/activate
which python
= ~/Projects/python/foo-project/env/bin/python
Use
deactivate
whenever using the same shell to swith between projects
So virtualenv
will use the default Python version installed (for me it was 2.7.10
).
To switch it to using Python 3, execute the following:
virtualenv -p python3 <env>