Create a virtual environment Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Virtual Environment?

A

“A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Benifits of a virtual environment

A

Your development environment is contained within your project, becomes isolated, and does not interfere with your system installed Python or other virtual environments
You can create a new virtual environment for multiple Python versions
You are able to download packages into your project without admin privileges
You can easily package your application and share with other developers to replicate
You can easily create a list of dependencies and sub dependencies in a file, for your project, which makes it easy for other developers to replicate and install all the dependencies used within your environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to Install a Virtual Environment using Venv

A

How to Install a Virtual Environment using Venv

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

To use venv in your project,

A

create a new project folder, cd to the project folder in your terminal, and run the following command:

python<version> -m venv <virtual-environment-name></virtual-environment-name></version>

like so:

mkdir projectA
cd projectA
python3.8 -m venv .venv

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to Activate the Virtual Environment

A

source .venv/bin/activate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is the Virtual Environment Working?

A

First we check the list of packages installed in our virtual environment by running the code below in the activated virtual environment. You will notice only two packages – pip and setuptools, which are the base packages that come default with a new virtual environment

run the code:

$ pip list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to Install Libraries in a Virtual Environment

A

To install new libraries, you can easily just pip install the libraries. The virtual environment will make use of its own pip, so you don’t need to use pip3.

After installing your required libraries, you can view all installed libraries by using pip list, or you can generate a text file listing all your project dependencies by running the code below:

run code:

$ pip freeze > requirements.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to make sure others can set up same environment

A

$ pip freeze > requirements.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is a requirements file important to your project?

A

Consider that you package your project in a zip file (without the env folder) and you share with your developer friend.

To recreate your development environment, your friend will just need to follow the above steps to activate a new virtual environment.

Instead of having to install each dependency one by one, they could just run the code below to install all your dependencies within their own copy of the project:

$ ~ pip install -r requirements.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to Deactivate a Virtual Environment

A

~ deactivate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly