Skip to content

Latest commit

 

History

History
107 lines (76 loc) · 3.65 KB

23_virtual_environment.md

File metadata and controls

107 lines (76 loc) · 3.65 KB

30 Days Of Python: Day 23 - Virtual Environment

Twitter Follow

Author: Asabeneh Yetayeh
First Edition: Nov 22 - Dec 22, 2019

<< Day 22 | Day 24 >>

30DaysOfPython

📘 Day 23

Setting up Virtual Environments

To start with project, it would be better to have a virtual environment. Virtual environment can help us to create an isolated or separate environment. This will help us to avoid conflicts in 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 which are specific for that project. Open your terminal and install virtualenv

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip install virtualenv

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

For Mac/Linux:

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project\$ virtualenv venv

For Windows:

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

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

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ ls
venv/

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

For Mac/Linux:

asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ source venv/bin/activate

For Windows:

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) asabeneh@Asabeneh:~/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's install flask to this project.

(venv) asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project$ pip install Flask

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

(venv) asabeneh@Asabeneh:~/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) asabeneh@Asabeneh:~/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: Day 23

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

🎉 CONGRATULATIONS ! 🎉 << Day 22 | Day 24 >>