What is a python virtual environment?
Most python project we develop will have dependencies, that’s code written by other people. We can install and use them so that we don’t have to rewrite it ourselves. For example Django library we use to create web apps and there are thousands of other written by other developers .To use them we have to install them. The libraries change over time as new features are developed. When we install it will be specific version and it may change after months and when we update it our code may not work so we have to change our code. By using VE we can separate the dependencies of one project from another that way we can have different version of same library .
A python virtual environment has two parts: python interpreter and third part libraries installed in it. These environments are separated from each other which means any change we make on one does not affect the other. This was we can install and use different python and library versions for different projects.
what is it used for?
As we saw above python virtual environments allow us to install and use different python versions and dependencies for our projects these in return has several uses. Some of it’s importance are:
- VE helps us to avoid dependency conflicts . Without VE when we update dependencies and python interpreter , we change the global python environment ,which means the previous code may not work as features and functionalities change. By using VE we can install and use different versions without affecting our previous projects.
- VE helps to create separated contexts to keep dependencies required by different projects separate so they don’t interfere with other projects or system-wide packages.
How to create a python VE?
To create a python VE first we need to to download and install python on our computer . After that we should create a folder and then create a VE inside it. We will look at different ways of creating a VE but first let us look at few packages we can use.
Packages for handling VE
There are some tools to work with python virtual environment. Some of these tools are;
- Venv – which comes pre-installed with python 3 or higher.
- Virtualenv – is a superset of venv which helps to create virtual environments more quickly.
- Conda – offers packages and dependencies for python and other languages.
How to create a python VE with Venv python 3?
You need to have python installed on your computer. First using the command line create a project folder and go to that directory. Then to create a VE in the current folder, write python version -m venv VEname and run.
Activating a python virtual environment on window
To activate to virtual environment write the following command on cmd and run
After activating the virtual environment, its name appears in parentheses at the start of the terminal prompt as showed below
Activating a python virtual environment on mac
To activate to virtual environment write the following command on cmd and run
How to deactivate
After you finish working with a virtual environment, or you want to change to another virtual environment, you can deactivate an environment by running the following command:
How to install packages on VE
Let’s see some examples of how to install third party packages on python VE. We can check the pre-installed packages on the virtual environment by running the pip list command.
We need to install other dependencies which are not pre-installed. Lets look at some examples:
Lets how to install selenium package. Selenium is a free (open-source) automated testing framework used to validate web applications across different browsers and platforms.
Lets take another example on how to install packages. Lets how to install Django package. Django is a popular python frame work to build web applications.
How to delete python VE
If you want to delete a virtual environment, you can simply delete its folder, no uninstall required.
How to use VE on python2.0
You can use Virtualenv tool to install python virtual environment on python 2.0. It is a tool used to create an isolated Python environment. You must have Python 2 and PIP installed on your system. Use pip2 to install virtualenv Python module.To install VE on python 2 follow the following steps:
- Get the full file path to the version of Python you just installed.
- Go to the directory you want to create your project. This is where you’ll create the new virtual environment.
- Create the virtual environment at the same time you specify the version of Python you wish to use. The command will be virtualenv path venvName
- Then to activate the VE run the command: activate.
Conclusion
In conclusion python virtual environment is a great tool to help keep our code base working while updating python and dependencies to new version and being able to use different version for separate projects.
Reference
- https:https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/
- https://blog.teclado.com/python-virtual-environments-complete-guide/
- https://realpython.com/python-virtual-environments-a-prime
Thanks bro!