Friday, July 23, 2021

Python Virtual Environment (WINDOWS)

What Is a Virtual Environment?

At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.

Remember do not put scripts inside virtual Enviroment

To get started, if you’re not using Python 3, you’ll want to install the virtualenv tool with pip:

#pip install virtualenv

If you are using Python 3, then you should already have the venv module from the standard library installed.

Note: From here on out, we’ll assume you’re using the newer venv tool, since there are few differences between it and virtualenv with regard to the actual commands

Create a project folder

#mkdir my-python-project
#cd my-python-project

To check modules using pip

#pip list

If in a virtualenv that has global access, do not list globally-installed packages

#pip list --local

To check built-in modules

#python
>>>help("modules")

Create virtual environment 

[Python 2:]
#virtualenv project_venv

[Python 3]
#python -m venv project_venv

Give the virtual environment access to the system site-packages dir

#python -m venv project_venv --system-site-packages

To activate the virtual Environment

#project_venv\Scripts\activate.bat

To check which binary is being used

#where python

To save the list of modules via pip

#pip3 freeze > requirements.txt

To save the list of modules via pip

#pip3 freeze --local > requirements.txt

To activate the virtual Environment

#deactivate

To remove the virtual Environment completely

#rmdir project_venv /s

To install same modules from different project from requirements.txt

First activate the virtual Environment

#pip install -r requirements.txt -v


Reference:

1. https://www.youtube.com/watch?v=APOPm01BVrk


Share:

1 comment:

  1. for LINUX and MAC
    https://www.youtube.com/watch?v=Kg1Yvry_Ydk

    ReplyDelete