Python : Virtual Environments
Hey, What’s that virtual environment ?
Let’s suppose that you are working on 2 python projects. One uses python version 3.4 and the other uses 3.7, how do you manage these types of conditions? Would you switch the version while working on each one?
The answer is no. You can create an environment for each project so that you can manage both simultaneously.
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.
Virtual environments include a fresh duplicate of Python binaries, and a standalone copy of the entire Python standard library. so you can deploy a Python app in a dedicated server and can share dependencies with others.
how can I set up a virtual environment?
Python3 has the default module venv to manage virtual environments ( from Python 3.3+ the venv package is included ). To create the environment, just move to your project directory, and run the following script:
$ python3 -m venv < your environment name >You can add your custom name for your virtual environment.
This will create the < your environment name > directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
For simplicity, let < your environment name >. name be test-env.
Activating Virtual Environment.
Once you’ve created a virtual environment, you may need to activate it.
On Windows, run:
$ test-env\Scripts\activate.batOn Unix or MacOS, run:
$ source test-env/bin/activateNote: you can carry your environment from one system having the same os. so other one doesn’t waste time to install the same. Pretty cool!
Deactivating Virtual Environment.
To deactivate, simply use the command:
$ deactivatehow can i manage the python versions here?
pyenv is the best method for python version management. pyenv lets you easily switch between multiple versions of Python. It will let you change the global and local Python versions easily.
You can use the below installation guide to install pyenv and to manage the python version.
https://github.com/pyenv/pyenv/blob/master/README.md
Install pyenv.
$ curl -L https://github.com/pyenv/pyenv- installer/raw/master/bin/pyenv-installer | bashNOTE: curl | bash is a risky command, make sure to execute them from trusted source only.
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrcNOTE: Since I am using ubuntu I used the command ~/.bashrc . You can change it to ~/.bash_profile, ~/.profile or ~/.bashrc. According to your os
Get Sarath Ravi’s stories in your inbox
Join Medium for free to get updates from this writer.
after the installation you can check for available versions.
$ pyenv versionsYou can check the available list of all python versions, with the following command:
$ pyenv install — listfor example,
$ pyenv install 3.9.5You can set the global python version and local python version (inside your environment ) using the following commands,
$ pyenv global 3.9.5$ pyenv local 3.9.5
How virtual environment works ?
After activating the Virtual Environment, your terminal will show something like this:
$ source env/bin/activate
(test-env) $ This means that you are in a virtual environment test-env.
if you set the python version inside your environment as 3.7 and globally as 3.9 .
(test-env) $ python --v
3.7.0Inside your environment, you can install the python packages using PIP which is a package manager used to install and manage Python packages.
(test-env)$ pip install < some package name >You can generate the dependencies file of your project using the command below:
(test-env)$ pip freezepip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects. A common convention is to put this list in a requirements.txt ( requirements.txt is standard file name you can use different names that doesn’t matter ) file:
(test-env)$ pip freeze > requirements.txtConclusion
Virtual environments, or “virtualenvs” are lightweight, self-contained Python installations designed to be set up with minimum fuss, and to “just work” without requiring extensive configuration or specialized knowledge.
At its core, the purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its dependencies, regardless of what dependencies every other project has.
In the example above, we created a separate virtual environment for both Project A and Project. Each environment, in turn, would be able to depend on whatever version of Project C they choose, independent of the other.
virtualenv avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way.
For more details you use the below links.
Thank You







