Virtual Environments
DIGU TRENDS TECH

Virtual Environments

Welcome to Digu Trends Tech, This is Day 24 of the series Master Python Programming in 30 Days, and today we will discuss Virtual Environments in Python. LET'S GO

Setting up Virtual Environments

To start with the 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

Saqlain@Digu:~$ pip install virtualenv        

Inside the 30DaysOfPython 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:

Saqlain@Digu:~/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.

Saqlain@Digu:~/Desktop/30DaysOfPython/flask_project$ ls
venv/        

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

For Mac/Linux:

Saqlain@Digu:~/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) Saqlain@Digu:~/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) Saqlain@Digu:~/Desktop/30DaysOfPython/flask_project$ pip install Flask        

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

(venv) Saqlain@Digu:~/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) Saqlain@Digu:~/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 24

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

All right, that's it guys for Day 24. Don't forget to practice each concept daily and If you have any doubts or questions, feel free to ask in the comment section.

fin.

要查看或添加评论,请登录

Saqlain Yousuf的更多文章

  • What is Docker?

    What is Docker?

    Welcome to Digu Trends Tech, today we are going to discuss what is Docker and how it simplifies application deployment…

  • Break, Continue, and Pass in Python

    Break, Continue, and Pass in Python

    Welcome to Digu Trends Tech, This is Day 25 of the series Master Python Programming in 30 Days, and today we will…

  • Python Web Scraping?

    Python Web Scraping?

    Welcome to Digu Trends Tech, This is Day 23 of the series Master Python Programming in 30 Days, and today we will…

  • Classes and Objects

    Classes and Objects

    Welcome to Digu Trends Tech, This is Day 22 of the series Master Python Programming in 30 Days, and today we will…

  • Regular Expressions

    Regular Expressions

    Welcome to Digu Trends Tech, This is Day 21 of the series Master Python Programming in 30 Days, and today we will…

    1 条评论
  • File Handling

    File Handling

    Welcome to Digu Trends Tech, This is Day 20 of the series Master Python Programming in 30 Days, and today we will…

  • Exception Handling

    Exception Handling

    Welcome to Digu Trends Tech, This is Day 19 of the series Master Python Programming in 30 Days, and today we will…

  • Python Error Types

    Python Error Types

    Welcome to Digu Trends Tech, This is Day 18 of the series Master Python Programming in 30 Days, and today we will…

  • Higher Order Functions

    Higher Order Functions

    Welcome to Digu Trends Tech, This is Day 17 of the series Master Python Programming in 30 Days, and today we will…

  • List Comprehension

    List Comprehension

    Welcome to Digu Trends Tech, This is Day 16 of the series Master Python Programming in 30 Days, and today we will…

社区洞察

其他会员也浏览了