Skip to content

Instantly share code, notes, and snippets.

@dreamorosi
Last active October 18, 2023 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreamorosi/e2947827e5de92b69df68c88475eba38 to your computer and use it in GitHub Desktop.
Save dreamorosi/e2947827e5de92b69df68c88475eba38 to your computer and use it in GitHub Desktop.
Create new Python virtual environment and manage dependencies

Create new Python virtual environment and manage dependencies

Requirements

Create new virtual environment

The following command creates a new virtual environment named venv in the current directory, usually this will be your project's directory.

$ virtualenv venv

Activate virtual environment

The following commands activate an existing virtual environment on Windows and Unix systems. The command assume that the virtual environment is named venv and that its location is in a subdirectory path/to/ of the current directory.

# Windows (CMD.exe)
$ path\to\venv\Scripts\activate.bat
# Unix
$ source path/to/venv//bin/activate

Once the virtual environment has been actiated your console cursor might prepend the name of the virtual environment as shown below.

$ (venv) echo 'Hello World!'

Deactivate virtual environment

The following command deactivates the current virtual environment, any dependency installed after this command will be installed globally.

$ (venv) deactivate

Manage dependencies in virtual environment

Dependencies can be installed in several ways, after having activated the virtual environment use the following command install a single dependency.

$ (venv) pip install boto3

The same command can be used to install multiple dependencies.

$ (venv) pip install boto3 psycopg2

And it can be used to install a specific version of a module, this works both with a single dependency and multiple dependencies and other logic operators can be used (<, >, <=, >=).

$ (venv) pip install boto3==1.9.125

When installing multiple dependencies or working on someone else's project it can be convenient to use a requirements.txt file. This file contains a list of all the dependencies needed for the project to work. Please take in account that if the file is malformed or some dependencies are not available the whole install process will fail and will need to be repeated. To install dependencies in the current environment from a requirements.txt file the command below can be used.

$ (venv) pip install -r requirements.txt

It is also possile to create a requirements file from the current environment, to do so run the following command while the virtual environment is active. This will create a file called requirements.txt (the name is just a convention) in the current directory.

$ (venv) pip freeze >> requirements.txt

Miscellaneous

To install dependencies using pip while being behind proxy add the following flags after pip install.

$ (venv) pip install --proxy http://user:pass@proxyAddress:proxyPort --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org boto3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment