-
Notifications
You must be signed in to change notification settings - Fork 2
Virtual Environment Guide
Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.
Python 3 comes bundled with the venv module to create virtual environments.
To manage multiple versions of python the most popular app is pyenv:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
-
curl https://pyenv.run | bash
you should see a message like
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
2. `nano ~/.bashrc`
3. paste the message into the file
4. restart the shell
P.S if you're using other shell like zsh
replace the bash
and bashrc
accordingly
pyenv install <version>
The new python version should be installed in the directory ~/.pyenv/versions/<version>
One of the most popular virtual environment manager is virtualenv
sudo apt-get install python3-pip
sudo pip3 install virtualenv
-
It's recommended to create the virtual environment in their respective folder
-
virtualenv -p <python file location> <virtual environment name>
ex:
virtualenv -p ~/.pyenv/versions/3.7.3/bin/python venv
Before you work on your project, activate the corresponding environment:
source venv/bin/activate
Your terminal will change to show the name of the activated environment.
deactivate
You need requirements.txt
file to keep track of all the installed packages in your app to create it, follow the following steps
- activate the virtual environment
-
pip freeze
This command shows you all the packages in your virtual environment -
pip freeze > requirements.txt
redirects all the package names with their versions into therequirements.txt
file