-->

How to use Python Virtual Environment - venv

 Virtual Environment

How to use Python Virtual Environment - venv

python virtual environment -venv

What is Virtual Environment?

Virtual Environment - we will call it (Virtualenv) - is an isolated environment where you can download packages away from python system packages.

This article is about venv which is a built-in python package, and for windows users.

How to simply use virtual environment?

* To create a virtual Environment folder:

>> python -m venv folder_name -> It's a good practice/common convention to add venv to the folder name to differentiate it from other folders.

OR:

>> python -m venv Exsisting_folder_name\new_venv_folder_name

* To activate/move inside the Virual Envirnment folder:

>> folder_name\Scripts\activate.bat

OR:

>> Exsisting_folder_name\new_venv_folder_name\Scripts\activate.bat

- You will notice the name of the Virtual Environment folder in the beginning of the folder path. 

- Now, you're working on an isolated folder which has only the default python global version installed on your computer + pip + setup tools.

- If you install any packages, they will be installed only in this environment/folder

* to see the installed packages inside your virtual environment:

>> pip list

- If you have installed some packages and want to collect them in a text file to use them in another environment, the test file name must be requirements.txt

>> pip freeze

- 'pip freeze' shows the installed packages and their dependencies in the correct format for the 'requirements.txt' file

* to copy the output/installed packages in the Virtual Environment folder to a txt file:

** This can be done in two ways:

1- >> pip freeze >requirements.txt    This will create the requirements.txt file inside the vrtual envronment folder

2- manually copy the output of the pip freeze, and then manually create the 'requirements.txt' file wherever you want and paste the output inside it in case you want to work on it outside the virtual environment folder.

* to deactivate the environment:

>> deactivate    You will notice that the name of the vitual environment folder has disappeared from the beginning of the folder path.

* to delete the virtual environment folder and it's subdirectories (be aware that requirements.txt file will be deleted too, unless you want to keep it for future use so move it or create it outside of the virtual environment folder):

>>rmdir virtualenvironment_folder_name /S

* to create a new folder in order to create the virtual_environment folder inside it(This is a good practice to avoide including your virtual environment source control into the project and let other people build their own environment): 

>>mkdir new_folder_name

>>python -m venv new_folder_name\New_virtual_environment_folder

* to activate the New_virtual_environment_folder:

>>new_folder_name\New_virtual_environment_folder\Scripts\Activate.bat

* to install the packages from a requirements.txt file (that you have saved before):

>>pip install -r requirements.txt   or (path_to_the_txt_file\requirements.txt) if you're on a different directory

* to create an environment with access to the system packages:

- first: delete the existing environment folder from your project folder:

>> rmdir virtual_environment_folder /S

- Second: create again a new virtual environment folder with below steps:

>>python -m venv New_virtual_environment_folder --system-site-packages

* to activate the new environment which we have created with the system packages

>>virtual_environment_folder\Scripts\Activate.bat

- Now, the additional packages you install inside this environment will not affect the system's packages.

* to list only the installed packages in this environment (separated from the system packages ):

>>pip list --local


Post a Comment

0 Comments