Python项目使用pyproject安装pytorch

In this post, I will show you how to use pyproject to install pytorch with specific cuda version using poetry and pipx.

What we will use

Actually, please ensure you have a python environment and pip installed.

install pipx

I use pip to install pipx in a system-wide python environment because pipx will automatically install the python packages separately.

Actually, if you don’t want pipx, you can use pip to install poetry directly.

pip install pipx

install poetry

Poetry is a tool for dependency management and packaging in Python.

It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Different from pip, it will create a virtual environment for you and install the packages in the virtual environment.

Actually, Poetry is used to manage a Python based project with pyproject.toml, and that’s how I use Poetry.

pipx install poetry

Now you can use pipx list to check if poetry is installed.

pipx list

# output like this
venvs are in /home/lap/.local/share/pipx/venvs
apps are exposed on your $PATH at /home/lap/.local/bin
manual pages are exposed at /home/lap/.local/share/man
   package poetry 1.8.4, installed using Python 3.12.7
    - poetry

using virtual env

poetry can build an virtual env using the pyproject.toml.

poetry env use python

# activate the poetry environment
poetry shell

Install pytorch with poetry

Because the pytorch just supplies with cuda pip, but we want to install the pytorch with specific cuda version and using poetry.

using poetry the add a source to the pytorch wheel files with specific cuda version.

poetry source add --priority=supplemental pytorch_cu124 https://download.pytorch.org/whl/cu124

Then add the pytorch package with the following command.

This is a large package so it will take some time to install. (Actually maybe 3.5GB because I using mobile data network to download accidentally 😵)

poetry add --source pytorch_cu124 torch torchvision torchaudio

try it

# try it  
python -c "import torch; print(torch.__version__)"
# output like this
2.5.0+cu124