pip #
Note
pip
is shipped with pretty much all Python installations (also conda).
As such one can use pip
in both conda
and pure Python installations.
See here for installing Python.
pip is the de-facto standard for installing and maintaining packages/modules in a Python environment. Its documentation is extensive. Here some common tips and tricks will be listed.
Tip
It is recommended to create virtual Environments, for basically everything. Testing software installations using Environments allows you to do everything in an area of the computer that does not interfere with the rest of the system.
pip will generally install packages without problems if one is in a clean environment (read, no prior installed packages). The longer time one uses pip in the same environment (virtual or not) the larger the risk is of causing problems via corrupted dependencies. This should not deter one from using pip at all, this is just to mention that doing updates on the fly may not always be a good idea without testing in a separate environment.
Installing packages#
Packages can be installed in a very simple manner, note how multiple packages and associated requirements can be specified in a single invocation of the program.
Note
pip list
will give you the current list of installed packages.
python -m pip install "numpy==1.24.*" "scipy<1.10" "matplotlib"
python -m pip install "numpy==1.24.*" "scipy<1.10" "matplotlib"
python3 -m pip install "numpy==1.24.*" "scipy<1.10" "matplotlib"
python3 -m pip install "numpy==1.24.*" "scipy<1.10" "matplotlib"
The above will install:
Note
Use pip3 show numpy
to get more detailed information about the numpy
package, such as directory where it is installed etc.
numpy at the latest version in the 1.24 release cycle
scipy at the latest version, but older than the 1.10
matplotlib at the latest version
when specifying more than one package on the line, all dependencies will be checked against each other at install time.
Note
You will quite often encounter some warnings or notices from pip
,
they look something like the following. It is perfectly normal, and
it is not necessary to do what it says.
pip
versions 21 and older:
pip
versions 22 and newer:
Requirements file#
Many Python tutorials will share a so called requirements.txt
file which contains
lines packages. For instance to replicate the installation shown in :ref`pip-installing`, one could create a file (called requirements.txt
):
numpy==1.24.*
scipy,1.10
matplotlib
Basically it contains the equivalent of many packages on the same line, see Installing packages.
Installation using a requirements.txt
file is simply specifying the file with -r
flag:
python -m pip install -r requirements.txt
python -m pip install -r requirements.txt
python3 -m pip install -r requirements.txt
python3 -m pip install -r requirements.txt
Dependencies and conflicts#
Installing packages over the course of several months. During this time, new releases and new dependency requirements of each package arise.
Tip
Always prefer to use virtual environments to reduce package conflicts.
Below is a constructed example of a dependency conflict arising.
$> python -m pip install dtumathtools==1.0.1
... lots of output
$> python -m pip install --upgrade numpy
... lots of output ... then
dtumathtools 1.0.1 requires numpy<1.24,>=1.21.1, but you have numpy 1.25.2 which is incompatible.
$> python -m pip install dtumathtools==1.0.1
... lots of output
$> python -m pip install --upgrade numpy
... lots of output ... then
dtumathtools 1.0.1 requires numpy<1.24,>=1.21.1, but you have numpy 1.25.2 which is incompatible.
$> python3 -m pip install dtumathtools==1.0.1
... lots of output
$> python3 -m pip install --upgrade numpy
... lots of output ... then
dtumathtools 1.0.1 requires numpy<1.24,>=1.21.1, but you have numpy 1.25.2 which is incompatible.
$> python3 -m pip install dtumathtools==1.0.1
... lots of output
$> python3 -m pip install --upgrade numpy
... lots of output ... then
dtumathtools 1.0.1 requires numpy<1.24,>=1.21.1, but you have numpy 1.25.2 which is incompatible.
The first command finalized package installation obeying all requirements between the installed
packages (dtumathtools
depends on many other packages which will also be installed).
The second command will also succeed, however, at the end a warning will be printed.
This tells us that the package dtumathtools
installed is version 1.0.1
and that it requires
a numpy
version in the range 1.21.1<=numpy<1.24
. However, the upgrade installed numpy
version
1.25.2
which is not in the requirement range. It still got installed regardless of the already installed
packages requirements! Sometimes this results in a broken installation and nothing works.
Warning
pip
only obeys package requirements for packages installed on the same installation command:
# this will install numpy and scipy in compatible versions
... pip install numpy scipy
# this MAY install numpy and scipy in non-compatible versions
... pip install numpy
... pip install scipy
To check possible conflicts in the current environment do
python -m pip check
python -m pip check
python3 -m pip check
python3 -m pip check
venv – environment #
venv – VS Code#
VS Code manages virtual environments via intrinsic VS Code commands.
Here is a brief explanation on how to setup a virtual environment in VS Code.
Warning
If you encounter problems with virtual environments in VS Code, please see here.
First check that you are not already using a virtual environment (see point 5. below).
Create a folder/directory according to the course you wish to work on.
This is optional, but recommended to retain a structure of your course activities.
Press Open Folder to start working in the designated course folder. If you already have several opened folders, this will not be visible. Just select the workspace.
Once you are in a folder/workspace we should create a virtual environment. Press Ctrl-Shift-P and search for
Python: Create Environment...
, and select that.Next, select the below option
Check that the virtual environment is functional. Sometimes you need to run a small Python script to enable the virtual environment, either press , or Shift-Enter
venv – Terminal#
The package venv creates environments using the pip method.
Once an environment has been created, and sourced, all pip
commands only act in that environment.
Note
The venv
package can only create environments with different packages.
It cannot switch between different Python versions.
An environment will be a directory on your computer. Deleting it will be the same as deleting the environment.
Warning
Environments in Visual Studio Code requires special handling when working in workspaces. Please see here.
An environment can be created using:
python -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python -m venv course-A
python -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python -m venv course-A
python3 -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python3 -m venv course-A
python3 -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python3 -m venv course-A
Once created, one can activate/use the environment by sourcing a file.
Change course-A
with the name of the environment.
Warning
Please see this FAQ entry!
course-A\Scripts\Activate.ps1
Warning
Please see this FAQ entry!
course-A\Scripts\activate.bat
source course-A/bin/activate
source course-A/bin/activate
Now every executed Python script will only use the packages installed
in the environment. And every pip
command will only install/remove packages
in the environment.
To get out of the environment, simply run the command deactivate
.
Below is a complete example of creating an environment, installing a specific package, running a code using the environment, and getting out of it.
python -m venv numpy-env
numpy-env\Scripts\Activate.ps1
pip install "numpy==1.23.*"
python -c "import numpy as np ; print(np.__version__)"
deactivate
python -m venv numpy-env
numpy-env\Scripts\activate.bat
pip install "numpy==1.23.*"
python -c "import numpy as np ; print(np.__version__)"
deactivate
python3 -m venv numpy-env
source numpy-env/bin/activate
pip install "numpy==1.23.*"
python3 -c "import numpy as np ; print(np.__version__)"
deactivate
python3 -m venv numpy-env
source numpy-env/bin/activate
pip install "numpy==1.23.*"
python3 -c "import numpy as np ; print(np.__version__)"
deactivate
virtualenv – environment#
The package virtualenv creates environments using the pip method.
Once an environment has been created, and sourced, all pip
commands only act in that environment.
Install it with pip using the package name virtualenv
.
virtualenv
is the original implementation of the venv
package. virtualenv
will be continuously developed and contains more up to date
methods and functionalities. As such virtualenv
can be updated while venv
is shipped with the Python sources and thus can not be updated.
The usage of virtualenv
is equivalent to venv
by replacing all venv
entries
with virtualenv
. Additional options can be found on the homepage.