Virtual Environments in Python
This tutorial topic is: Virtual Environments in Python. We will learn how to Create Virtual Environments.
A virtual environment is a space in which the interpreter, libraries, and scripts for python are installed and are isolated. It is a directory tree that contains Python files that can be executed. While programming in python, one requires the use of certain packages and modules that often don’t come in the standard library. Sometimes, python applications may even require a certain version of the above packages and modules.
You may learn: How to create your own package in python
We can thus conclude that it may not be possible for one python installation to meet the requirement of every single application. Henceforth comes into play the need for a virtual environment. By this, I mean that Suppose a project requires version 1 of an application and another project requires version 2 of the same application as the bugs have been fixed in the updated version. Both the projects can have different virtual environments with different versions of the application while implementation.
How to Create Virtual Environments?
- Open the command prompt.
- See if you have python added to Your PATH environment variables. In the command prompt, type the following command:
C\:~ > python
- If the above result is not displayed, it means that Python is not added to the PATH variable. If it is not added to the PATH variable, then go to Control Panel>System and Security>System>Advanced System Settings. Under the Advanced section choose Environment Variables. Then Under System Variables, there will be a variable with the name ‘PATH‘.Click on it and then click on Edit; After inserting a semicolon on the current text, add the current location of the Python.exe file.
- Now, open the Command line and follow the steps below:
C\:~ > mkdir Mydir C\:~ > cd Mydir C\:~\Mydir> virtualenv hello C\:~\Mydir> hello\Scripts\activate https://drive.google.com/open?id=1B_VuMeQDKVqYbHDmDVWeMhpMoC2VgxyD
Step – By – Step Explanation
- In the command prompt, the command ‘mkdir’ (make directory) is for making a new directory (in other words a file folder) and then comes the name of the newly to-be-created directory. Here, we created a directory with the name ‘Mydir’. This file can later be found in the location stated before ‘>’.
- The ‘cd’ (change directory) command is to change the current directory to the directory with the name after this command. Here, we are changing the storage location to the directory ‘Mydir’ using the ‘cd’ command.
- Then we are using the command ‘virtualenv’ to create a virtual environment with the name ‘hello'(here).
- Then we activated the virtual environment ‘hello’.