- Published on
Python Virtual Environment
- Authors
- Name
- Saurabh Prakash
- @saurabhp75
Virtual environmnet workflow
# Install virtualenv module
$ pip install --user virtualenv
# Create a new virtual env
$ py -m venv env
# Activate the new virtual env
$ .\env\Scripts\activate
# Install all the required packages from requirements.txt
$ pip install -r requirements.txt
# De-activate the new virtual env
$ deactivate
Using virtualenvwrapper
It’s advantage is that it keeps all virtual env in one place (
~/.virtualenvs
)pip install --user virtualenvwrapper
Add these lines in
.bashrc
export VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3" source ~/.local/bin/virtualenvwrapper.sh
source
~/.bashrc
Create a virtual environments
$ mkvirtualenv --python=python3.6 virt-env-name
List virtual envs.
$ lsvirtualenv
Enter virtual environments
$ workon virtual-env-name
Exit virtual env
$ deactivate
Delete/remove a virtual environment
$ rmvirtualenv ENVNAME
Virtualenvwrapper (From test driven develoment book)
Install virtualenvwrapper
$ pip install --user virtualenvwrapper
Put these lines in
.bashrc
export VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3" source ~/.local/bin/virtualenvwrapper.sh
Create virtual environment
$ mkvirtualenv --python=python3.6 virt_env_sname
Conda and virtual environments
Create a virtual environment
$ conda create --name $ENVIRONMENT_NAME python
Activate an environment
$ source activate $ENVIRONMENT_NAME
Deactivate an environment
$ source deactivate
Create requirements file
$ conda list --export
List all environments
$ conda info --envs
Python virtual environments using pyenv
Install your preferred Python version (to be run only once)
$ pyenv install 3.6.5
Make it a virtualenv so you can make others later if you want
$ pyenv virtualenv 3.6.5 myenv
Make it globally active (for your user)
$ pyenv global myenv
Delete the virtualenv
$ pyenv uninstall myenv
Make a new one
$ pyenv virtualenv 3.6.5 myenv
Make environments active per-directory
$ pyenv local myenv
will drop a .python-version
file into your current folder and any time you invoke Python or pip-installed Python utilities from it or under it, they will be shimmed by pyenv.
Source : https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get/865644#865644
Create a virtual environment for a specific Python version.
$ pyenv virtualenv 3.4.3 my_env_3_4
Create a virtual environment based on the current Python version.
$ pyenv version 2.7.10 (set by /Users/akbar/.pyenv/version) $ pyenv virtualenv my_env_2_7
This will create a virtual environment based on Python 2.7.10 under the pyenv root in a directory called my_env_2_7.
List existing virtual environments created by pyenv.
$ pyenv virtualenvs
The pyenv versions command will also show the virtual environments along with the installed versions of Python.
$ pyenv versions
If eval "$(pyenv virtualenv-init -)" is configured in your shell, pyenv-virtualenv will automatically activate/deactivate virtualenvs on entering/leaving directories which contain a .python-version file that contains the name of a valid virtual environment as shown in the output of pyenv virtualenvs. For manual activation/deactivation, see 5 and 6.
To manually activate a virtual environment.
$ pyenv activate my_env_2_7
To manually deactivate a virtual environment.
(my_env_2_7)$ pyenv deactivate
Delete a virtual environment.
$ pyenv uninstall my_env_2_7
Source : http://akbaribrahim.com/managing-python-virtual-environments-with-pyenv-virtualenv/