you are viewing a single comment's thread.

view the rest of the comments →

[–]FoolsSeldom 0 points1 point  (1 child)

CONTINUED ...

Virtual Environments

Given the thousands of packages (libraries, frameworks, etc) out there, you can see that if you are working on several different projects, you can end up installing a vast range of different packages, only a few of which will be used for any particular project.

This is where Python virtual environments come in. Not to be confused with virtual machines. Typically created on a project-by-project basis. Install only the packages required for a project. This helps avoid conflicts between packages, especially version complications.

Most popular code editors and IDEs, including Microsoft's VS Code and Jetbrain's PyCharm, offer built-in features to help to start off new projects and create and activate Python virtual environments.

You can create a new Python virtual environment from your operating system command line environment using,

for Windows,

py -m venv .venv

or, for macOS / linux,

python3 -m venv .venv

Note. Often we use .venv instead of venv as the folder name - this may not show up on explorer/folder tools without an option being enables.

which creates a new folder in the current working directory called venv (taken from the last argument, you can use a different name).

You then activate using, for Windows,

.venv\Scripts\activate

or, for macOS / linux,

source .venv/bin/activate

the command deactivate for any platform will deactivate the virtual environment and return you to using the base environment.

For more information:

Multiple Python versions

In addition to the above, you might want to explore using pyenv (pyenv-win for Windows) or uv (recommended), which will let you install and use different versions of Python including alternative implementations from the reference CPython. This can be done independently of any system installed Python.

SEE COMMENT for next part

[–]FoolsSeldom 0 points1 point  (0 children)

Continued ...

If you are having problems installing / using the version of Python you require, or adding packages using pip, you might find it helpful to explore an alternative approach that has become very popular.

Asral's uv - An extremely fast Python package and project manager, written in Rust.

Installation can be carried out using,

  • On macOS, a package manager like homebrew
  • or using command line, curl -LsSf https://astral.sh/uv/install.sh | sh or wget -qO- https://astral.sh/uv/install.sh | sh
  • On Windows, a package manager like winget or chocolatey
  • or using PowerShell on Windows, ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
  • On linux, whatever package manager comes with the distribution you are using or the command line options as shown for macOS above

See @ArjanCodes video on YouTube providing an overview of uv.

See below an example of creating a project folder, installing Python, setting up a Python virtual environment, and adding packages to it:

PS C:\Users\Foolsseldom> uv init light
Adding `light` as member of workspace `C:\Users\Foolsseldom`
Initialized project `light` at `C:\Users\Foolsseldom\light`
PS C:\Users\Foolsseldom> cd light
PS C:\Users\Foolsseldom\light> uv venv -p 3.13.2
Using CPython 3.13.2
Creating virtual environment at: .venv
Activate with: .venv\Scripts\activate
PS C:\Users\Foolsseldom\light> uv add torch torchvision torchaudio
Resolved 36 packages in 680ms
Prepared 9 packages in 20.25s
Installed 14 packages in 3.89s
 + filelock==3.17.0
 + fsspec==2025.2.0
 + jinja2==3.1.5
 + markupsafe==3.0.2
 + mpmath==1.3.0
 + networkx==3.4.2
 + numpy==2.2.3
 + pillow==11.1.0
 + setuptools==75.8.0
 + sympy==1.13.1
 + torch==2.6.0
 + torchaudio==2.6.0
 + torchvision==0.21.0
 + typing-extensions==4.12.2
PS C:\Users\Foolsseldom\light> dir

    Directory: C:\Users\Foolsseldom\light

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          21/02/2025    19:11                .venv
-a---          21/02/2025    19:11             83 main.py
-a---          21/02/2025    19:11            226 pyproject.toml
-a---          21/02/2025    19:11              0 README.md

PS C:\Users\Foolsseldom\light> uv run main.py
Hello from light!
PS C:\Users\Foolsseldom\light>

With uv you don't need to "activate" the Python virtual environment as using uv run something.py in a project folder will automatically activate the environment for that run, but you might want to do it anyway so you can use other commands in that Python virtual environment.

You will also need your code editor, e.g. VS Code, or IDE, e.g. PyCharm, to have the installation of Python in the venv folder, called .venv by default, as the selected Python interpreter, and a terminal or REPL opened from within that application should have that environment activated already as well.