Skip to content

Instantly share code, notes, and snippets.

@sec-fortress
Last active February 25, 2024 21:14
Show Gist options
  • Save sec-fortress/dc55e447246f58ef03357bd2f8468291 to your computer and use it in GitHub Desktop.
Save sec-fortress/dc55e447246f58ef03357bd2f8468291 to your computer and use it in GitHub Desktop.
Creating a Python Virtual Environment

Python venv (Virtual Environment) is a tool that comes bundled with Python 3.3 and later versions. It allows you to create isolated environments for Python projects.

To create a virtual environment you need to use the python venv module

python3.9 -m venv myenv

We need to need to activate the environment variable so that when we run Python or install packages, it happens within the virtual environment.

  • For windows run the below command
myenv\Scripts\activate
  • For Linux/Mac run the below command
source myenv/bin/activate
  • Python commands we run will use the Python interpreter and packages installed within that environment.
  • We can install packages using pip, and they will be installed only within the virtual environment.
  • Does not affect your global Python installation.

When you're done working within the virtual environment, you can deactivate it with the following command:

deactivate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment