Virtual environments¶
Due to the vast amount of packages available, and different packages using different versions, we strongly recommend working in a virtual environment. In a virtual environment, your packages can be installed in an isolated environment without causing issues with packages installed in other environments. You can e.g. have one version of a package in one environment, and other versions in another environment. It is best practice to always install the packages into a virtual environment.
Some common virtual environments are pip environments
, conda environments
, and venv
.
They are largely interchangable, but we recommend starting with uv (which uses venv internally) or pipenv, unless you have installed Python through conda/anaconda.
uv¶
uv is a fast Python manager, which is quickly gaining traction within the Python community. Install instructions are listed above, and can be found at the official homepage.
After uv has been installed, you can use uv to install a specific python version, e.g. version 3.13: uv install python 3.13
.
Typically, it is good to initialize a uv controlled project by running uv init
in the folder, which will create some necessary files. Then, to create a new virtual environment, use uv venv
.
Packages can be added with uv add <packagename>
, or with uv pip install <packagename>
. To run a script with your environment, either activate the environment with uv venv
and then run your script with python main.py
, or directly with uv run main.py
.
A neat thing about uv, is that it can auto setup your environment (if the necessary uv files are already in place, and packages have been installed with uv add
), by directly running uv run main.py
. This makes it easy to share code with each other and ensure that the python environment is identical.
uv cheat-sheet
## install uv on Linux/Mac
curl -LsSf https://astral.sh/uv/install.sh | sh
## create new project
uv init myproj
## install packages
uv add django requests "pandas>=2.3"
## remove package
uv remove django
## see pkg dependency tree
uv tree
## run a python script directly w/o starting venv
uv run main.py
## install specific version of python
uv python list
uv python install 3.12
## start a new project and pin it to Python 3.12
uv init myproject
uv python pin 3.12
uv add django
uv run main.py #(will automatically install py3.12 and django into venv)
## run a cli tool like Ruff
uv run tool ruff #(or uvx ruff)
## update uv version to latest
uv self update
Pip environments¶
To get started, install pipenv
:
pip install pipenv
Then, create a new environment (or reactivate an existing environment) by running:
pipenv shell
This will create an environment if not existing, and then launch the virtual environment shell. Note that the name of the virtual environment will be the same as the folder that pipenv shell
was run from, although the name is not important.
To relaunch a virtual environment, simply go to the same directory and run pipenv shell
again.
virtualenv¶
virtualenv
is similar to pip environments, except for that the virtualenv environment is created as a subfolder in the project folder. You need to navigate into the virtual environment folder and launch the shell from there, and then go back up again.
To create a new environment you can do it in one of the following ways
-
Using virtualenv
First install
virtualenv
using pippip install virtualenv
Create your environment
virtualenv "NameOfEnvironment"
Note that you can specify which python you want to use with the
--python
option, followed by the python path.virtualenv --python python "NameOfEnvironment"
-
Using python venv
Alternatively, you can use
venv
through python, see below. This option is built in to python and you do not need to install anything extra. However, note thatvirtualenv
is more flexible and includes more features.python -m venv "NameOfEnvironment"
A general tip could be to start the name with a dot, i.e. .Name
, to easier distinguish the venv folder from your regular folders.
Then to activate the environment you need to call activate
.
./NameOfEnvironment/Scripts/activate
deactivate
Note that in the case of using git, git is going to track the venv folder if it is in a git environment. You can add the folder to your .gitignore
file to avoid this, another way is to create the virtual environment outside the git project.
You can read more about virtualenv and venv.
Anaconda and Conda environment¶
Update: Recently, usage of Anaconda has become controversial, due to the fact that it switched from being free to being non-free for academic use. We therefore no longer support using anaconda
Anaconda is a distribution of Python and R programming languages primarily used for data science, machine learning, and scientific computing. It includes various tools and libraries pre-installed, making it easier for users to set up a Python environment for data-related tasks.
Conda environments are best when python is installed using conda/anaconda. Conda has its own repository of packages besides the ones in pip, which are typically pre-compiled. These are thus often more common when using large packages like machine learning packages.
You can read more about conda environments here.
Key Features:
- Package Management: Anaconda comes with its own package manager called conda, which simplifies the process of installing, updating, and managing Python packages and dependencies.
- Environment Management: Users can create isolated Python environments using conda, allowing them to manage different sets of packages and dependencies for different projects.
- Pre-installed Libraries: Anaconda includes popular data science and machine learning libraries like NumPy, pandas, SciPy, scikit-learn, TensorFlow, and Jupyter.
- Navigator: Anaconda Navigator provides a graphical interface for managing environments, installing packages, and launching applications such as Jupyter Notebook and Spyder.