Installing Python on Windows with pyenv
Up to date, my preferred method to work with Python on Windows was Miniconda. It is a small distribution that can be tied to the very extensive conda-forge repository. It has the advantage that it has a single script conda
that allows managing environments, installing software and upgrading all software in an environment. Unfortunately, due to firewall restrictions it seems my organization no longer can access these repositories, which is why I am forced to use other sources.
This blog post documents an alternative method that only relies on one tool pyenv-win
.
Step 1: Install pyenv
We are going to install pyenv-win, a small tool to install Python releases and switch between versions of the interpreter. Following the instructions from the web, we will open a PowerShell window and type
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" rm install-pyenv-win.ps1
Close the terminal and open it again to have access to the new tool. You can verify that the tool is installed
C:\Users\user>pyenv pyenv 3.1.1 Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands duplicate Creates a duplicate python environment local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version update Update the cached version DB rehash Rehash pyenv shims (run this after installing executables) vname Show the current Python version version Show the current Python version and its origin version-name Show the current Python version versions List all Python versions available to pyenv exec Runs an executable by first preparing PATH so that the selected Python which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help <command>' for information on a specific command. For full documentation, see: https://github.com/pyenv-win/pyenv-win#readme
Run pyenv update
and then install one version of the interpreter. You may look at the Python download page to see which versions are available with which levels of maturity, but first also confirm that the version is available with pyenv install -l
. In my case I chose 3.10.11 which is the last version with a security maintenance status, setting it also as a the global version of python to use
pyenv install --quiet 3.10.11 pyenv global 3.10.11
After this, we can check it works
> pyenv version 3.10.11 (set by C:\Users\juanj\.pyenv\pyenv-win\version) > python -c "import sys; print(sys.executable)" C:\Users\juanj\.pyenv\pyenv-win\versions\3.10.11\python.exe
Step 2: Create an environment
When working with python software, it is customary to create a unique environment that contains only the libraries and tools the software needs. This allows you as a developer to keep track of the requirements of your software and which versions of the libraries are used, making the process more reproducible for other users and for yourself, should you move to a different computer.
Assuming you have set up a new Python project in the folder c:\Users\me\src\myproject
, what you would do now is to use your pyenv
installation to create a fresh new environment in that particular folder
> cd c:\Users\me\src\myproject > python -m venv .venv
This creates a subfolder, with name .venv
that contains a link to a particular version of python and the pip
tool to install the software.
You can activate this software from the command line. When using the Windows command line processor this looks as follows:
C:\Users\me\src\myproject>.venv\Scripts\activate.bat (.venv) C:\Users\me\src\myproject> python -c "import sys; print(sys.executable)" C:\Users\me\src\myproject\.venv\Scripts\python.exe
When using PowerShell it changes a bit:
PS C:\Users\me\src\myproject> & c:/Users/me/src/myproject/.venv/Scripts/Activate.ps1 (.venv) PS C:\Users\me\src\myproject> python -c "import sys; print(sys.executable)" C:\Users\me\src\myproject\.venv\Scripts\python.exe
Note how in both cases the current environment (.venv)
appears at the beginning of the prompt, indicating where our Python software is installed.
Now it is the time to install whatever libraries your software needs. Ideally, you should create a requirements.txt
file, with a list of the libraries, which in my case would read
numpy scipy matplotlib
and then install it using pip
(.venv) PS C:\Users\me\src\myproject> pip install -r requirements.xt
Step 3: Install and use Visual Code
I personally recommend using Visual Studio Code as a development environment for Python on Windows, for various reasons:
- Once you install this editor and open a Python file (with extension
.py
), it will suggest to install the editor's tools for handling Python as a language, including syntax verifiers andPylance
as language server to autocomplete and verify the code. - If you have created a Python environment local to your project, as explained above, you can select this environment to check, test and run your software.
- If you tell VS Code to use other Python tools, such as autoformatting with
black
, or opening Jupyter Notebooks, the editor will upgrade your Python environment with additional tools to handle those tasks transparently.