Tech

Python in 30 Days: Day 23 – Virtual Environment

Day 23: Setting up Virtual Environments

To start with the project, it would be better to have a virtual environment. A virtual environment can help us create an isolated or separate environment. This will help us avoid conflicts over dependencies across projects. If you write pip freeze on your terminal, you will see all the installed packages on your computer. If we use virtualenv, we will access only packages that are specific to that project. Open your terminal and install virtualenv

tech@Tech:~$ pip install virtualenv

Inside the 30 Days of Python folder, create a flask_project folder.

After installing the virtualenv package, go to your project folder and create a virtual env by writing:

For Mac/Linux:

tech@Tech:~/Desktop/30DaysOfPython/flask_project\$ virtualenv venv

For Windows:

C:\Users\User\Documents\30DaysOfPython\flask_project>python -m venv venv

I prefer to call the new project venv, but feel free to name it differently. Let us check if the the venv was created by using ls (or dir for windows command prompt) command.

tech@Tech:~/Desktop/30DaysOfPython/flask_project$ ls
venv/

Let us activate the virtual environment by writing the following command at our project folder.

For Mac/Linux:

tech@Tech:~/Desktop/30DaysOfPython/flask_project$ source venv/bin/activate

Activation of the virtual environment in Windows may very on Windows Power shell and git bash.

For Windows Power Shell:

C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\activate

For Windows Git bash:

C:\Users\User\Documents\30DaysOfPython\flask_project> venv\Scripts\. activate

After you write the activation command, your project directory will start with venv. See the example below.

(venv) tech@Tech:~/Desktop/30DaysOfPython/flask_project$

Now, lets check the available packages in this project by writing pip freeze. You will not see any packages.

We are going to do a small flask project so let us install flask package to this project.

(venv) tech@Tech:~/Desktop/30DaysOfPython/flask_project$ pip install Flask

Now, let us write pip freeze to see a list of installed packages in the project:

(venv) tech@Tech:~/Desktop/30DaysOfPython/flask_project$ pip freeze
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

When you finish you should dactivate active project using deactivate.

(venv) tech@Tech:~/Desktop/30DaysOfPython$ deactivate

The necessary modules to work with flask are installed. Now, your project directory is ready for a flask project. You should include the venv to your .gitignore file not to push it to github.

Exercises: Python in 30 Days: Virtual Environment

  1. Create a project directory with a virtual environment based on the example given above.

<< Day 22 | Day 24 >>

Tech G

View Comments

  • Your article helped me a lot, is there any more related content? Thanks!

Recent Posts

Python in 30 Days: Day 30- Conclusions

Day 30 Conclusions In the process of preparing this material, I have learned quite a…

10 months ago

Python in 30 Days: Day 29 – Building an API

Day 29: Building API In this section, we will cover a RESTful API that uses HTTP…

10 months ago

Python in 30 Days: Day 28 – API

Day 28: Application Programming Interface (API) API API stands for Application Programming Interface. The kind…

10 months ago

Python in 30 Days: Day 27 – Python with MongoDB

Day 27: Python with MongoDB Python is a backend technology, and it can be connected…

10 months ago

Python in 30 Days: Day 26 – Python for web

Day 26: Python for Web Python is a general-purpose programming language, and it can be…

10 months ago

Python in 30 Days: Day 25 – Pandas

Day 25: Pandas Pandas is an open-source, high-performance, easy-to-use data structure, and data analysis tool…

10 months ago