Skip to content

Instantly share code, notes, and snippets.

@pizofreude
Last active July 26, 2023 09:55
Show Gist options
  • Save pizofreude/e45cb46e1313ad6d8530f26c20ff2ff2 to your computer and use it in GitHub Desktop.
Save pizofreude/e45cb46e1313ad6d8530f26c20ff2ff2 to your computer and use it in GitHub Desktop.
Python Virtual Environment Setup

Python Virtual Environment

Best practice before creating python project to avoid dependencies error across programs is to code in virtual environment.

Setup

Go to desired directory in the terminal, then type in command line:

Windows:

python -m venv virtualEnvironmentName

p/s: Shorter name is better like venv or virt. For example:

python -m venv venv

Mac:

python3 -m venv virtualEnvironmentName

A directory of virtualEnvironmentName/ should be made in the selected path.

Activate the virtualEnvironmentName via the terminal:

source virtualEnvironmentName/Scripts/activate

Now, the (virtualEnvironmentName) should be shown in the Terminal above the directory:

venv

Once the venv activated, continue working like normal in local machine.

Deactivate the virtualEnvironmentName via the terminal:

deactivate

The output will be: Notice the (virtualEnvironmentName) is deactivated and not shown after the deactive command.

venv2

Install libraries

Show Installed Libraries

pip freeze

For a newly setup venv, nothing should be returned as it just freshly built.

After installing via pip install <libraryName>, the output should be e.g.:

venv3

Creation of requirements.txt

At the end of the project, do this:

  • Windows:
pip freeze > requirements.txt
  • Mac/Linux:
pip3 freeze > requirements.txt

So that, other user can simple recreate the venv via:

  • Windows:
pip install -r requirements.txt
  • Max/Linux:
pip install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment