When is an unique directory for virtual environments used? Which
option is recommended? Why?
Every virtual environment “lives” in its own folder. All packages you install will go there, especially if every environment will have a different Python version.
How should I install per-project packages listed above?
When you switch to the project environment after you created it, see my original answer below. All packages installed will exclusively be installed into that virtual environment you are currently working in.
You can always check which Python interpreter is currently in use by typing
which python
in the terminal you currently have the the project environment activated. In addition you can also check
which pip
to make sure if you are installing using pip install somepackage
that you target the correct Python interpreter. If you want to pin the packages, you can do
pip freeze > requirements.txt
any time and the currently installed packages plus their version will be written to the textfile requirements.txt
. You can now always create a new environment using
pip install -r requirements.txt
Should I use virtualenvwrapper?
I would always work in a per-project virtual environment, so other projects that may use some pinned version of a special package are not influenced.
How do I switch between projects (changing Python/virtual-environment
in the process) easily or painlessly?
You could define an alias in your ~/.bashrc file or ~/.bash_aliases
. In a terminal, open (in my example) the ~/.bashrc
with a text editor, e.g., Vim/nano or one of your liking:
nano ~/.bashrc
and somewhere near the end you can add a line with an alias to switch to the project directory and activate the environment at the same time:
alias activate_proj1="cd ~/project_1 && pyenv activate venv_project_1"
so you only type activate_proj1
in the terminal (tab completion also works) and both commands are executed. Don’t forget to source the bash-file again after you change something with source ~/.bashrc
or just open a new terminal.
Original answer:
pyenv
will handle everything you need:
My workflow (for one project to make it more readable) would be the following:
pyenv install 3.5.1
cd python_projects
mkdir myproject
cd myproject
pyenv virtualenv 3.5.1 venv_myproject
After that you can simply activate the virtualenv created by pyenv
using
pyenv activate venv_myproject
which will open your distinct environment. Here you can do all the things you want, e.g., install your packages using pip etc.
After you completed setting up the environment, you can freeze the environment and create a requirements file:
pip freeze > requirements.txt
to be able to reconstruct the environment if needed. This way all the overhead that may be needed (setting a PATH etc.) will be handled by pyenv.
If you want to work on different projects, just activate the environment you need and off you go!
Note that you can make pyenv
activate the virtualenv when you cd
the folder in your terminal by putting its name into your .python-version
file as well.