Mastering Python Virtual Environments: A Guide to Using ‘python -m venv’

rajneesh

rajneesh

4 min read

  • python
python-virtual enviroment complete guide

are you a python developer. are you work on a many project that projects work on different packages of different versions and you need a different environments for each project then don`t warry because i am going to a complete guide how can you create virtual environment for each project easily. for this method i follow different resources like python docs, stack overflow, GitHub etc. and i have create a very easy guide for you by which you can easily setup virtual environment. i have 100% sure after Go through this post you no need to any other post or guide.

Introduction

in a python project development, we need to create a isolated workspaces for project its importance. Virtual environments allow developers to manage dependencies, versions, and packages in a clean, organized manner. in this post we are complete learn how to use virtual environments venv module like how to create, activate, and management of this powerful tools in windows, mac and Linux.

for example, you are working to two different project where for project 1 required ‎Django 4.2 and project 2 required ‎Django 5.0 then you can use virtual environments for each project.

What is a Virtual Environment?

virtual environment is a python module which have a self-contained directory that create a isolated python virtual environment with specific Python interpreter along with a collection of installed packages. this method ensure that each project its own unique set of dependences and avoid conflicts between project as well as maintain the consistency across for development and production environment.

Why we Use Virtual Environments?

  • Isolation: its isolation each project by dependencies separate.

  • Control: Manage different versions of packages as well as Python interpreter itself.

  • Reproducibility: you can Share and collaborate with others without thing about version conflicts.

  • Convenience: you can easily switch between the different project without worried about dependencies breaking.

Creating a Virtual Environment

before creating a virtual environment you need to install virtual environment module by this command:

pip install virtualenv

Test your installation:

virtualenv --version

first of all navigate to your project’s directory where you want to create virtual environment then open that directory in terminal and run the following command:

python -m venv .venv

This command creates a .venv directory in your project folder, which containing the Python executable and a copy of the pip package manager. you can use pip to install other packages in virtual environment. The name of the virtual environment is .venv you can use any other name according to you by replacing the .venv with you Preferred name. so that you get virtual environment by that name.

Activating the Virtual Environment

before using the virtual environment you need to activate this environment .to activate this env you need to open you terminal or shell’s to that path where env is created so that the Python interpreter and scripts for the virtual environment can use.

For Windows:

.venv\Scripts\activate

For macOS and Linux:

source .venv/bin/activate

once you can activate your venv then your command prompt is change to indicate that you are now working within the virtual environment. you can see this in below image.

Installing Packages

With in the virtual environment activated, you can install packages using pip. These packages will be local to the virtual environment and won’t affect the system Python installation .

pip install <package_name>

Deactivating the Virtual Environment

To exit the virtual environment and return to the system’s default Python interpreter, you can simply run this command:

deactivate

some important commands

use can you below command to see all the install packages or module on that env with version:

pip list

if you want to share/export using virtual environment packages you can use this command for creating a requirements.txt that have you all packages details inside this file.

pip freeze > requirements.txt

Best Practices

  • Requirements File: Use a requirements.txt to keep track of your dependencies.

  • Version Control: Include your requirements.txt in version control, but exclude the virtual environment directory.

  • Regular Updates: Keep your packages up-to-date to benefit from security patches and new features.

Conclusion

By mastering the use of python -m venv, you’re taking a significant step towards more reliable, maintainable, and collaborative Python projects. every python developer must know about the module or package you can share this post with you python developer friends which help allot during creating a python projects. i covered every point in very easy way by which you can easily understand the concept. if you like my post you can follow me and comment you experience will creating venv.

rajneesh

rajneesh

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.