Dark Mode
Image
  • Thursday, 12 December 2024
The Complete Guide to Python Virtual Environments, activate, deactivate & Requirements.txt

The Complete Guide to Python Virtual Environments, activate, deactivate & Requirements.txt


Python-2: The Complete Guide to Python Virtual Environments, activate, deactivate & Requirements.txt - Outline

1.1. Introduction

In this tutorial, you’ll learn how to work with Python’s virtual environments for your projects. Each environment can use different versions of package dependencies and Python. After you’ve learned to work with virtual environments, you’ll know how to help other programmers reproduce your development setup, and you’ll make sure that your projects never cause dependency conflicts for one another.

Virtual environments are a common and effective technique used in Python development. Gaining a better understanding of how they work, why you need them, and what you can do with them will help you master your Python programming workflow.

 

 

1.2. Why need Virtual Environment?

When developing software with Python, a basic approach is to install Python on your machine, install all your required libraries via the terminal and write all of the source code. This works fine for simple Python scripting projects.

To implement complex software development project, it is required a Python interpreter, Python library, an API, or software development kit. Often you will be working with multiple files, multiple packages, dependencies and as well as your source code. As a result, you will need to isolate your Python development environment for that particular project. This isolated environment is known as Virtual Environment. In this tutorial, I would like to present a clear explanation about Virtual Environment and how can you implement it on your python project.

 

 

1.3. What is Virtual Environment?

According to the Python's official documentation says: "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".

Python virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project and makes it easily reproducible.

Now time to implement the virtual environment for your python project. Here I would like to know you that Python virtual environment manager: either venv or virtualenv. Each module is the preferred way to create and manage isolated virtual environments.

  • venv is included in the Python standard library and requires no additional installation.
  • virtualenv is includes in the Python standard library and some others additional library installation.

 

 

1.4. Check Python

Open CMD to your project folder.

Write “python” to check the python version. Also write a simple python code to check it’s work fine. Now exit it to return this python environment.

$ python
>> print("hello")
>> exit()

Check the python version individually

$ python --version

Check the pip version

$ pip -V

Upgrade the pip version

$ python -m pip install --upgrade pip

To check the list of packages with the version currently installed.

$ python -m pip list

 

1.5. Install & Uninstall Virtual Environment

Then just install with this command.

$ pip install virtualenv

Uninstall virtualenv with this command.

$ pip uninstall virtualenv

 

 

1.6. Virtual Environment by “virtualenv”: python -m virtualenv name_of_virtual_environment

Create Virtual Environment with this command.

E:\code\python\tvenv $ python -m virtualenv myenv

Activate Virtual Environment by CMD in windows OS

E:\code\python\tvenv $ .\myenv\Scripts\activate.bat

Activate Virtual Environment by PowerShell in windows OS

E:\code\python\tvenv $ .\myenv\Scripts\Activate.ps1

Create requirments text file

(myenv) E:\code\python\tvenv $ pip freeze > requirments.txt

Install python libraries according to the requirments text file

(myenv) E:\code\python\tvenv $ pip install -r requirments.txt

Deactivate Virtual Environment by CMD in windows OS

(myenv) E:\code\python\tvenv $ .\myenv\Scripts\deactivate.bat

 

 

1.7. Virtual Environment by “virtualenv”: py -3 -m virtualenv name_of_virtual_environment
    
E:\code\python\tvenv $ py -3 -m virtualenv myenv
E:\code\python\tvenv $ cd myenv\Scripts
E:\code\python\tvenv\myenv\Scripts $ activate.bat
(myenv) E:\code\python\tvenv\myenv\Scripts $ deactivate.bat

 

 

1.8. Virtual Environment by “venv”: python -m venv name_of_virtual_environment
 
E:\code\python\tvenv $ python -m venv myenv
E:\code\python\tvenv $ cd myenv\Scripts\
E:\code\python\tvenv\myenv\Scripts $ activate.bat
(myenv) E:\code\python\tvenv\myenv\Scripts $ deactivate.bat

 

 

1.9. Virtual Environment by “venv”: py -3 -m venv name_of_virtual_environment
 
E:\code\python\tvenv $ py -3 -m venv myenv
E:\code\python\tvenv $ cd myenv\Scripts
E:\code\python\tvenv\myenv\Scripts $ activate.bat
(myenv) E:\code\python\tvenv\myenv\Scripts $ deactivate.bat

 

 

1.10. Requirments.txt

Create requirments text file. This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions.

(myenv) E:\code\python\tvenv $ pip freeze > requirments.txt

Install python libraries according to the requirments text file. Later if you need to re-create the environment, it will be easier for a different developer to install the same packages using the same versions:

(myenv) E:\code\python\tvenv $ pip install -r requirments.txt

This can help to ensure consistency across installations, deployments, and developers.

 

 

Comment / Reply From