0% found this document useful (0 votes)
23 views

Python Packages and Virtual Environments

asgafdgsdfafdgdfgadfgdfga

Uploaded by

micogec858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Packages and Virtual Environments

asgafdgsdfafdgdfgadfgdfga

Uploaded by

micogec858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Packages and Virtual Environments

Python packages are important to extend the functionalities of the basic Python instal-
lation. On pypi.org (the Python Package Index) you will find over half a million different
Python packages for all sorts of purposes. For instance, the package pandas is often used
by Data Scientists for data analysis and manipulation, and the package matplotlib is
often used for data visualization.
To make use of such packages, we need to do two things: First, the package must be
installed on the system. Installation is something that needs to be done only once.
(However, you may want to upgrade the package after some time to make use of additional
features added by the package authors). Second, you need to import the package in the
concrete Python script or Jupyter Notebook that you are working on.
We can install Python packages via the package manager pip using the command line.
By default, the package is installed in the global environment, which means that this
installation affects all projects that use the global environment. This is something that we
usually try to avoid. Instead, it is good practice to install packages into a dedicated vir-
tual environment for the specific project we are working on. In this way, the technical
setup of our current project will be isolated from the setup of other projects which may
need entirely different Python packages (e.g. Pandas), package versions (Pandas version
2.1) or even Python versions (Python 3.9). We thereby try to ensure that projects are
lean, reproducible, and produce no conflicts with other projects.

1 Virtual environments
In the context of university courses, it makes sense to create one virtual environment
per course. To create a virtual environment for the precourse programming, first create
a dedicated folder (e.g. “precourse”) in which you will store all materials of this course.
Open this folder in VS Code via File > Open Folder > Select the precourse folder. Now,
you should only see the contents of this folder in VS Code’s file explorer. And if you
open a terminal, you will see that the terminal is pointed to the precourse folder. Next,
we create a virtual environment.
Option 1: Creating virtual environments using VS Code’s graphical user in-
terface
Open the Command Palette (Ctrl+Shift+P), search for the Python: Create
Environment command, and select it. Use the Python package venv to create
the virtual environment.

1
You will see a list of Python interpreters to choose from as a base for the new virtual
environment. If you just have the current Python version installed, you will only see
Python 3.12 shown here.

After selecting the Python version, a new folder .venv will be created and activated
in the current directory. This folder contains the virtual environment which consists in
particular of the Python executable file and the Python standard library.
Option 2: Creating virtual environments from the Terminal
Alternatively, you can create a virtual environment using the Terminal via the following
command:

python -m venv .venv

To activate the virtual environment, use the following command:

.venv\Scripts\activate # Windows
source .venv/bin/activate # Mac/Linux

Note: Windows users may run into an an error, because the execution of scripts is by
default deactivated on Windows Powershell. If this is the case, you need to activate script
execution via the command Set-ExecutionPolicy -ExecutionPolicy Unrestricted
-Scope CurrentUser

2 Package Installation with pip


Once, we have created and activated the virtual environment, we can install packages
using the package manager pip via the command pip install package_name (e.g. pip
install pandas). Often this will not only install this one package, but also further pack-
age dependencies. To install multiple packages provide the package names separated by
blanks (pip install pandas numpy matplotlib). There are several other important
pip commands, in particular:

pip --help # Get help


pip install -r requirements.txt # Install from requirements file
pip install package_name --upgrade # Upgrade a package
pip list # List all installed packages
pip freeze > requirements.txt # Create requirements file
pip check # Check for package compatibility
pip show package_name # Show package information

2
3 Requirements file
A requirements file is a simple text file that lists all the packages that are required for a
project. It is a good practice to create a requirements file for each project. This file can
be used to install all the required packages at once, and it can be shared with others to
ensure that they have the same packages installed.
A simple requirements.txt file might look like this

pandas # Requirements without version specifiers


scikit-learn == 1.3.1 # Version Matching: must be version 1.3.1
matplotlib >= 3.12 # Minimum version 3.12

If the requirements.txt file is placed in the root directory of the project, you can install
all the required packages at once using the following command:

pip install -r requirements.txt

Whenever you are installing further Python packages, it is a good practice to update
the requirements file accordingly. This can be done using the command pip freeze >
requirements.txt. This command will overwrite the existing requirements file with the
currently installed packages and their versions. Note however, that the requirements file
will contain all installed packages, including those that are not necessary for the project.
It is therefore a good practice to review the requirements file and remove unnecessary
packages before sharing it with others.

4 Further notes
Note that there are several other ways to manage local environments, which may come
with their specific advantages and disadvantages. Some of the most popular tools are:

• Conda: The package manager conda is also often used in data science projects. It
is particularly useful for the installation of packages that have non-Python depen-
dencies, e.g. for packages that require C++ libraries.
• Virtualenvwrapper: This tool is useful if you want to store all virtual environ-
ments in one central directory. It allows you to create and switch between virtual
environments with a single command.
• Poetry: Poetry is an all-in-one tool for Python project management that inte-
grates dependency resolution, environment management, and package publishing,
aimed at simplifying and streamlining the development workflow. Useful for larger
projects.

You might also like