Virtual Env on Linux
Virtual environments enable you to have an isolated space on your server for Python projects,
ensuring that each of your projects can have its own set of dependencies that wonât disrupt any of your other projects.
Linux Set up
Setting up a programming environment provides greater control over Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages. While there are a few ways to achieve a programming environment in Python, weâll be using the venv module here, which is part of the standard Python 3 library. Letâs create the virtual environment (venv) by typing:
sudo apt install -y python3-venv mkdir environments cd environments python3 -m venv my_env source my_env/bin/activate
Create folder to hold virtual environment
With this installed, we are ready to create environments. Letâs either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir as in:
mkdir environments
Navigate to the directory
cd environments
Initialize virtual environment
Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:
python3 -m venv my_env
Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command
These files ensure project isolation, preventing system and project files from mixing. It's crucial for version control and providing specific package access to each project. Python Wheels, a package format in Ubuntu 20.04, can speed up software production by reducing compilation frequency. To activate this environment, run the following command that calls the activate script.
Activate the virtual environment
source my_env/bin/activate
This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environmentâs settings and packages.
Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.
Setting Up Python a Virtual Environment on Windows
Managing multiple Python versions
We are going to install a very similar module on Windows to manager our virtual environemnts on Python.
Pyenv does not officially support Windows and does not work in Windows outside the Windows Subsystem for Linux. Moreover, even there, the Pythons it installs are not native Windows versions but rather Linux versions running in a virtual machine -- so you won't get Windows-specific functionality.
Install pipenv
If you're in Windows, we recommend using @kirankotari's pyenv-win fork -- which does install native Windows
Quick Steps
pyenv versions pyenv install ver_num pyenv local ver_num virtualenv my_env ./my_env/scripts/activate Pip install package_name
Python Package Management
We are going to use pyenv to install the python version we want and together with virtualenv we will manage our python environments.
The commands below work for both Linux and Windows.
Show available python versions to download
pyenv install -l
Check all available pythons versions
This command will check which versions we have avaiable to choose from, if you need another version run the command pyenv install ver_num
pyenv versions
Installing a new python version
pyenv install 3.7 pyenv install 3.10.8
Set local python version
This will set up the version for the local virtual environment. The local variable will be the executable environment of our application, and its controlled outside the scope of the global python instalation already on our system
Pyenv local 3.7
Set Global version
We can also modify the global executable version of Pyhon, this will be which pacage of pythonn the applications on our system will use. For development, you do not want to modify the global python scope, but the option is available.
Check Virtual Env Executable
This command will show where the virtual environment is running from.
pyenv which python
On Windows I could not get it to work properly, however when executing the following it gave me the correct indication it was working. Run the command python on the termina, so a python interpreter will open up.
When you are inside the interpreter run the following
import system sys.executable
You will see that the executable of python is being directed into your folder,which is exactly what we want.
Get Environment set up
Create Virtual Environment
After picking which python version to use, we will now create the virtual Envinronment which will have all of our modules and dependencies for our project. Keeping all the modules organised in this way will benefit us in the long run and make our project much easier to manage and also to share with other developers.
python -m venv my_env
Activate Virtual Environment
After creating the environemnts what we want is to activate it and start installing some juicy modules
.\myvenv\Scripts\activate
On Linux use the below
Source venv/bin/activate
Installing Modules
We can simply run the usual command to install the modules we want.
pip install module_name
Managing Modules
After (un)installing any libraries using pip or modifying the files in a version's folder, you must run pyenv rehash to update pyenv with new shims for the python and libraries' executables.
pyenv rehash
Check Modules inside VE
When you have the virtual environment activated, we can run the following command to check the packages that are installed. The packages will be installed inside the /lib folder, also any other dependencies for those packages will be placed there.
pip freeze
Deactivate Environment
Whenever you close your environment your session will close, and the virtual environment will also terminate. There are however, occassions when you wish to terminate the virtual envionrment itself without having to close your session. Run the below to accomplis this.
cd .\myvenv\Scripts\ deactivate
Conclussion
If you made throught here congratulations! You have now mananged to create a virtual environment on Python, install pyenv to manage the python versions, and learned how to keep all your modules and dependencies organised.