How to use pipenv to create virtual environments
Pipenv is a package and dependency manager for Python projects. It harnesses the power of different existing tools, bringing their functionalities together:
- pip for Python package management
- pyenv for Python version management
- Virtualenv for creating different virtual Python environments
- Pipfile for managing project dependencies
Pipenv is preinstalled on all of Scaleway’s AI Docker images for GPU Instances. Launching one of these Docker images puts you in an activated pipenv virtual environment, with all your favorite packages and tools preinstalled. You can also use pipenv to create your own virtual environments, if you wish to go beyond the preinstalled Python environments already provided with the various Docker images.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have created a GPU Instance
- You have generated your SSH key
Accessing the pipenv virtual environment
See our dedicated documentation on how to access the pipenv virtual environment from your Scaleway GPU Instance.
Managing packages with pipenv
You can view packages, install/uninstall packages and update packages with a few simple pipenv
commands:
-
Enter the following command to see all the packages and dependencies installed in this environment, and their versions:
pipenv graph
-
Enter the following command to install a new package:
pipenv install <package>
Tip:To install a specific version of a package, use the command
pipenv install <package>~=1.2
-
Enter the following command to uninstall a package:
pipenv uninstall <package>
-
Enter the following command to update a package:
pipenv update <package>
Tip:To update all packages, simply run
pipenv update
Important:Be aware that installing and updating packages may provoke conflicts with the existing virtual environment installation.
Understanding Pipfiles
Every pipenv virtual environment has its own Pipfile. The Pipfile details the dependencies of the project, taking the place of the typical requirements.txt
file. When you create a Pipenv environment, a Pipfile is automatically generated.
-
From within the Docker container, in the
~/ai
directory, typels
to list all files in this directoryThe output should be similar to the following:
Pipfile Pipfile.lock -
Enter the command
cat Pipfile
to view the contents of Pipfile. The following information will help you understand the output:source
specifies the sources where available packages are added/installed frompackages
lists the packages required for the project (production and development environments)dev-packages
lists the packages required for the project (development environments only)requires
specifies the required python version for the project
Any packages that you explicitly install with the command
pipenv install <package>
will automatically be added to the Pipfile. If you push your project to a Git repository, other users who clone it will be able to install all dependencies, based on the Pipfile, with the commandpipenv install
. -
Enter the command
cat Pipfile.lock
to view the contents of Pipfile.lock.This file specifies which specific versions of the packages in the Pipfile should be used. This prevents packages which depend on each other being automatically upgraded, which could break your project dependency tree.
Creating your own virtual environments
-
Ensure you have connected to your GPU Instance via SSH and then launched a Scaleway AI Docker container.
You are now in the
~/ai
directory of the Docker container, with the virtual environment activated. -
Type
exit
to exit the virtual environment. -
Type
cd ~
to leave the~/ai
directory and go to the home directory. -
Create a new project directory for your new virtual environment, and navigate into it. Replace
my-proj
with the desired name for your project.mkdir my-proj && cd my-proj
Tip:To avoid losing your virtual environment upon exiting the container, make sure you create this folder in a directory that is mapped to one of your GPU Instance’s local volumes
-
Create a new virtual environment and generate a Pipfile:
pipenv install
-
Activate the virtual environment:
pipenv shell
Your command prompt should now be similar to
(my-proj) jovyan@442a38468c20:~/my-proj
, showing that you are in the activatedmy-proj
pipenv virtual environment.
Further help
See the official Pipenv documentation for further help with Pipenv.