Translate

Create new Python Virtual environment

 

Create a new virtual environment

 Python 3 allows you to manage separate package installations for different projects. It creates a “virtual” isolated Python installation. When you switch projects, you can create a new virtual environment which is isolated from other virtual environments. You benefit from the virtual environment since packages can be installed confidently and will not interfere with another project’s environment.

To create a virtual environment, go to your project’s directory and run the following command. This will create a new virtual environment in a local folder named .venv:

py -m venv .venv

Steps:

  1. Create a new directory and make cwd
  2. create the virtual directory I use: python -m venv .venv
  3. You can activate virtual environment inside of VSCode:
    • Ctrl/Shift/P
    • click on select interpreter
    • choose your newly created virtual environment (will be remembered, for all code in project)
  4. when using python from command line, start virtual environment as follows:
  1. Use cd to go to top directory of project or manually run batch file from bin folder, it will activate your virtual environment.
  2. If not using step 4 use Issue command . ./venv/bin/activate

How to check which virtual env is currently active

To check which virtual env is currently active run following command in python console window-

where python

Output: it will give path of python.exe which is currently active.

For more details, please refer HERE

No comments:

Post a Comment

Popular Posts