Installing Python¶
At your own computer¶
To run Python projects on your own computer, you will need to:
- Install Python
- A way of creating/managing virtual environments
- Install a C-compiler (optional if not doing modelling)
Install Python¶
Windows might redirect python to the windows store
If you are running Windows 10 1905 or newer, you might need to disable the built-in Python launcher via Start > "Manage App Execution Aliases" and turning off the "App Installer" aliases for Python
We recommend that you install python via uv or pyenv-win, so that you will be able to use virtual environments with different Python versions should it be needed.
The installation instructions are available at the official uv homepage. On most recent versions of Windows, packages can be installed via the terminal using winget:
winget install --id=astral-sh.uv -e
We will copy some of the install instructions from the pyenv-win how to install GitHub page here below, but we refer to the official install instructions should there be problems.
Installing via PowerShell¶
The easiest way to install pyenv-win is to run the following installation command in a PowerShell terminal:
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
If you are getting any UnauthorizedAccess error as below then start Windows PowerShell with the "Run as administrator" option and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine, and then re-run the above installation command. Note that changing the execution policy could potentially lower the security of your system. If you want to check your current execution policy level, before changing to RemoteSigned run Get-ExecutionPolicy in the PowerShell window.
For more information on 'digitally signed' or 'Security warning' you can refer to following issue #332
Installation of pyenv is now complete! The next step is to set up Python.
Setup python¶
First, update the list of available python version:
pyenv update
Then list the available python versions:
pyenv install --list
Then, install a version of python (e.g. 3.13.1):
pyenv install 3.13.1
Then, set the python version as your current python version:
pyenv global 3.13.1
pyenv local 3.13.1
We recommend using uv to manage python over, but if you still want to use it, instructions are below.
This version of the install will allow you to use virtual environments, but you will not be able to use different versions of python.
First, install python from the Windows store. One way is to open a new terminal (Windows Terminal or powershell), and running python. This will either take you to the store or start python if python is already installed.
Setup paths: To be able to use all packages you install, you need to add the folder where the packages get installed to to your path. You can run the script below in a powershell window to find and add the path automatically:
$scriptPath = Get-ChildItem -Path $env:LOCALAPPDATA -Recurse -Filter "Scripts" -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*PythonSoftwareFoundation*" } |
Select-Object -ExpandProperty FullName -First 1
if ($scriptPath) {
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";$scriptPath", [System.EnvironmentVariableTarget]::User)
"Scripts directory added to PATH: $scriptPath"
} else {
"Scripts directory not found."
}
To check that it works, install pipenv and check that it works:
pip install pipenv
pipenv shell
This should now put you in a virtual environment. You can verify this by checking the path to python in the current shell:
python -c "import sys; print(sys.executable)"
You should get a path similar to C:\Users\<user>\.virtualenvs\<folder-name>-NpavKrNU\Scripts\python.exe
You can remove the created environment by running pipenv --rm. Close the shell.
Now, open a new terminal/powershell and navigate to the folder where you have/will have your script, and the run pipenv shell to create a virtual environment. Now you should be able to find the virtual environment in e.g. Visual Studio Code.
On macOS, you probably already have python installed. Still, we recommend setting up a Python installation which can handle different version of Python via uv or pyenv. Otherwise, it is also possible to use the regular python and virtual environments (venv).
The installation instructions are available at the official uv homepage. On a macOS, uv can be installed via curl:
curl -LsSf https://astral.sh/uv/install.sh | sh
or via wget:
wget -qO- https://astral.sh/uv/install.sh | sh
Make sure that you have also pip installed:
pip install --upgrade pip
Note: on some versions of macOS, we need to run using pip3 instead of pip and python3 instead of python:
pip3 install --upgrade pip
Or potentially in the following way:
python3 -m pip install --upgrade pip
On Linux, you probably already have python installed. Still, we recommend setting up a Python installation which can handle different version of Python via uv or pyenv. However, it is technically possible to use the regular python and virtual environments (venv).
The installation instructions are available at the official uv homepage. On Linux, you can often find uv with your standard package manager. If not, uv can be installed via curl or wget:
curl -LsSf https://astral.sh/uv/install.sh | sh
or via wget:
wget -qO- https://astral.sh/uv/install.sh | sh
Make sure that you have also pip installed:
pip install --upgrade pip
Note: on some versions of macOS, we need to run using pip3 instead of pip and python3 instead of python:
pip3 install --upgrade pip
Or potentially in the following way:
python3 -m pip install --upgrade pip
Setting up a virtual environment¶
You can find more information about the virtual environments at this page. Below, we give a quick intro to uv, pyenv/pipenv, and venv
Do not create the environment in cloud storage (OneDrive/iCloud etc)
We have noticed that creating and running python scripts from within a cloud synced folder often causes problems. Also, these errors are not always clear. Therefore, we recommend storing the environment (and python project) in another folder, preferably in you home folder.
How do I get the virtual environment in the right folder?
The easiest way to manage where the project should be, is to first create the folder in the file explorer where you want it. Then, in that folder, right-click on the empty space, and select "Open in terminal". This will open a terminal session at the folder location.
A good location is typically in C:\users\<user>\projects\<this-project-name>, typically accessible in the terminal (after creation) via
cd ~\projects\<this-project-name>
Then, you can go ahead and create the environment using the instructions below.
The easiest way to manage where the project should be, is to first create the folders where you want them to be using the terminal.
A good location is typically in ~/projects/<this-project-name>. To create this folder, with a project folder named sysbio-projectrun
mkdir -p ~/projects/sysbio-project
if you want to navigate to this folder later, use:
cd ~/projects/<this-project-name>
You can verify that you are in the correct folder by running:
pwd
Then, you can go ahead and create the environment using the instructions below.
The easiest way to manage where the project should be, is to first create the folders where you want them to be using the terminal.
A good location is typically in ~/projects/<this-project-name>. To create this folder, with a project folder named sysbio-projectrun
mkdir -p ~/projects/sysbio-project
if you want to navigate to this folder later, use:
cd ~/projects/<this-project-name>
You can verify that you are in the correct folder by running:
pwd
Then, you can go ahead and create the environment using the instructions below.
After uv has been installed, you can use uv to manage python versions (uv python install ...), virtual environments (uv venv) and packages (uv add) with a single tool. First, start by installing/updating python to the latest version:
uv python install 3.13
If you have been given a project (i.e. a folder with a pyproject.toml file in it), you can just navigate to that folder and then setup everything automatically using:
uv sync
If you are starting a new project, it is typically best to initialize a new project. This will create a template project and setup all files needed to control the project environment. Just make sure to run the command from the folder where you want project files to be located. Verify the location first:
pwd
If the location is what you expected, initialize the project:
uv init
Packages can now be added with uv add <packagename>. To run a script with your environment use uv run main.py, or activate the environment with uv venv and the run your script with python main.py.
uv complains that the environment is shadowed
Please note that uv is able to find uv instances further up in the file-tree. Which means that if you accidentally created an environment further up, you need to remove that before installing packages. Remove all uv.lock and .venv files from that top-location.
If you accidentally created the environment in the default location when opening a new terminal, the files will most likely be in C:\users\<user>\ .
You can e.g. use the file manager to navigate to the folder where the files are to remove them. Perhaps you might need to show hidden files to be able to see the .venv folder.
If you accidentally created the environment in the default location when opening a new terminal, the files will most likely be in ~ .
On macOS, if using the Finder to see the files, you might need to enable seeing the files on the drive, or use "go" and go to "Home" folder to get to the files. Do not put the files in an icloud folder, this will likely cause problems. Perhaps you might need to show hidden files to be able to see the .venv folder.
You can also use the terminal to find the files and remove the using rm <file-name>.
If you accidentally created the environment in the default location when opening a new terminal, the files will most likely be in ~ .
If you are not using uv, we recommend managing environments using pipenv. Make sure to run the command from the folder to you want the virtual environment to be related to:
Note: on some versions of macOS, we need to run using pip3 instead of pip and python3 instead of python:
pip install pipenv
pipenv shell
Finally, test the setup by installing a package, e.g. numpy
pip install numpy
If you are not using uv, we recommend managing environments using pipenv. Make sure to run the command from the folder to you want the virtual environment to be related to:
Note: on some versions of macOS, we need to run using pip3 instead of pip and python3 instead of python:
pip install pipenv
pipenv shell
Finally, test the setup by installing a package, e.g. numpy
pip install numpy
Create the environment:
python3 -m venv .venv
And then activate it:
Powershell/terminal:
.venv\Scripts\Activate.ps1
command prompt:
.venv\Scripts\activate
source .venv/bin/activate
source .venv/bin/activate
Installing a C-compiler¶
When doing modelling (e.g. in thesis works) it is possible to get much faster simulation by compiling the models to C-code. To do this you need to have a valid C-compiler. Below we offer some quick steps of installing a valid compiler on Windows, Linux or macOS.
The full official Windows instructions are available here, but in short you can either install the smaller standalone compiler (Build Tools for Visual Studio 2022/2019), or the full version of visual studio. We recommend using the standalone compiler.
On most modern Windows installations, you can download and install the compiler needed via winget. Open a powershell (or Windows terminal) shell and run
winget install Microsoft.VisualStudio.2022.Community --override "--wait --quiet --add ProductLang En-us --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended"
Note that the installation will take a long time without much feedback, so have patience. Also note that you will likely get a popup asking for elevated permissions.
Alternatively, you can download the Build Tools for Visual Studio 2022/2019 installer, and run it. Make sure to install Desktop development with C++ or alternatively the C++ build tools and ensure the latest versions of MSVCv143 - VS 2022 C++ x64/x86 build tools and Windows 10 SDK.
Download the full version installer of Visual Studio, make sure to install Python development, Python native development tools, and Windows SDK (which can be found under Native development).
Also, make sure that you install Desktop development with C++ and make sure that the latest version of MSVC is checked. You might have to open the visual studio installer again and "modify" the installation to check if the compiler is installed.
If the installer doesn't detect your toolset version, Set $env:PlatformToolset before building.
Minimal command line tools install (recommended)¶
To get a C-compiler, it is typically enough with the Xcode command line tools. You can get these by opening a new terminal and running:
xcode-select --install
A popup should appear, follow the instructions. The installation might take a while, so have patience. You can can test that the installation worked by running clang --version in a terminal. For a more extensive test, see below.
The full Xcode install¶
If this did not work, you might need the full Xcode from the app store. After installation, start Xcode and agree to the license agreement.
You probably already have a compiler available, otherwise install e.g. gcc through your package manager.
Verifying the installation of Python and the compiler¶
The easiest way to test if the installation of Python and the compiler was successful is by installing our SUND toolbox and use its built-in test function that automatically creates and compiles a model:
uv run --with sund python -c "import sund; sund.test_distribution()"
pipenv install sund
python -c "import sund; sund.test_distribution()"
pipenv install sund
python -c "import sund; sund.test_distribution()"
pip install sund
python -c "import sund; sund.test_distribution()"
If all is working, you should see output similar to this:
================================================================================
✅ DISTRIBUTION TEST PASSED: All sund modules imported successfully, and model simulation completed without errors.
================================================================================
At a computer in a LiU computer hall (Linux)¶
If you are at a LiU shared computer running Linux (such as in the SU halls, or when logged in via ThinLinc), python3 should already be available. You can test with:
python3 --version
To setup a virtual environment in the computer halls to be able to install packages, you should do:
python3 -m venv .venv
source .venv/bin/activate
Then you can run python as normal, and use pip. For example to update pip:
python -m pip install --upgrade pip
Google Colab¶
Google Colab is a service where you can run Python code on a remote server. This can be good to share examples and do some initial testing. However, you need to know that data/files are not being saved, and thus you need to save and download the data from somewhere else. This makes Google Colab good for demo-purposes, but not useful for project work.