The python virtual environment is a mechanism to handle the different versions of python package dependencies of projects by isolating them from other python project package dependencies. Some of the uses of the virtual environment in python are described below. Further, I will demonstrate how to create a virtual environment in python. I will show how to install the flask package using a virtual environment. Then finally I will show you how to run the flask project using a virtual environment.
Use of the virtual environment in python
Some of the reasons for using the virtual environment in python projects are listed below.
- The python virtual environment is used for handling different versions of python packages dependencies.
- It isolates the python project dependencies from each other projects. A python package available in one environment is not accessible to other environments.
- It prevents the installation of all python packages in its default directory. Instead, with help of the virtual environment, you can install python packages in your choice of location.
- It makes the uninstallation of python packages very easy. You can just delete the virtual environment. It will delete all the packages related to a particular virtual environment project.
How to create and use it
In order to create a virtual environment in python, you have to install the python package called virtualenv. You can install the package by using the command given below.
$ pip3 install virtualenv
Once it is installed, you can check its version by using the command given below.
$ virtualenv --version
Now, you can create the virtual environment by using the command given below.
$ virtualenv virtualenv-name
The name of the virtual environment is given as virtualenv-name in the above command. But you can give any name as per your wish. The recommendation would be to give the name which describes the purpose or project that it is created for. Once it is created, you have to activate it. You can activate the virtual environment using the command given below.
$source virtualenv-name/bin/activate
Once it is activated, you can install python packages inside it. Once it is activated you can see the virtual environment name at the beginning of the terminal inside parenthesis. When you are done, you can deactivate it either by closing the terminal or by using the command given below.
(virtualenv-name)$ deactivate
How to create flask project with the virtual environment in python
Let’s create a simple hello world flask project. In order to create the flask project, I will make use of a virtual environment. So that whatever packages are used for this project it is bundled inside one container or environment. I will give the name of the project as “hello-world”.
$ mkdir hello-world
$cd hello-world
Now, let’s create the virtual environment for this flask project. I will name the virtual environment “venv-hello-world”. Then I will activate it to use it.
$ virtualenv venv-hello-world
$ source venv-hello-world/bin/activate
Hello World in Flask
Finally, you can install the flask using the command given below. You can also check the version of the flask.
(venv-hello-world) $ pip3 install flask
(venv-hello-world) $ flask --version
You can create a simple hello world flask project as follows. First, create an “app.py” python file.
(venv-hello-world) $ touch app.py
The content of the “app.py” file is given below.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<H1>Hello World</H1>"
Run the flask project to see the output.
(venv-hello-world) $ flask run
By default, the flask application will run on the address “http://127.0.0.1:500/”. You can open that address in the browser. The output will be the webpage with “Hello World” written on it. More detail about the flask can be found on the official website of the flask.
You can install the other python packages for your flask project. Once you are done, you can deactivate the virtual environment using the command given below.
(venv-hello-world) $ deactivate
Now, try to again run the flask project using the command given below. It will give an error saying the flask command is not found.
$ flask run
You might have noticed that I have created the virtual environment inside the “hello-world” directory. It is not compulsory to create a virtual environment inside the project directory. You can create anywhere and use it. But as good practice, you can always keep the virtual environment inside the project directory. So that you will know which virtual environment is used for which project. I would recommend you all to create the virtual environment inside the project directory.
Conclusion
In this blog article, we have seen the uses of the virtual environment in python. The main use of the virtual environment in python is to manage the different versions of python package dependencies across the different python projects. Further, you have seen how to deploy the flask application using the virtual environment.
You may be interested to learn about the following topics. Check them out.
I am an enthusiastic tech guy. Ever ready to learn new technology. I love building software solutions that can help mankind to solve problems.
You can support us by:
Becoming a patron at https://www.patreon.com/drukinfotech
Buy me coffee at https://www.buymeacoffee.com/drukinfotech