How to setup Python, PyEnv & Poetry on Windows

Setting up a robust Python development environment on Windows involves installing Python itself, managing multiple versions with PyEnv, and handling dependencies and project environments with Poetry. This guide will walk you through each step to get your system ready for efficient and organized Python development.

Prerequisites

  • Basic understanding of command-line operations.
  • Windows operating system (Windows 10 or later is recommended).

Step 1: Installing Python

  1. Download the Python Installer: Visit the official Python website and download the latest version of Python.
  2. Run the Installer:
    • Double-click the downloaded installer.
    • Ensure you check the “Add Python to PATH” option.
    • Choose “Customize installation” to select the features you need (optional).
    • Click “Install Now” to proceed with the installation.
  3. Verify the Installation: Open Command Prompt (cmd) and type:
python --version

This should display the installed Python version.

Step 2: Installing PyEnv

PyEnv is a tool that allows you to easily switch between multiple versions of Python.

Install Git for Windows: PyEnv requires Git. Download and install Git from git-scm.com.

Install PyEnv for Windows:

Open PowerShell as an administrator and run the following command to install PyEnv:

Invoke-WebRequest -UseBasicParsing -Uri https://pyenv-win.github.io/pyenv-win/install-pyenv-win.ps1 | Invoke-Expression

Update Environment Variables: Add the following paths to your system environment variables:

  • C:\Users\<Your-Username>\.pyenv\pyenv-win\bin
  • C:\Users\<Your-Username>\.pyenv\pyenv-win\shims

Restart PowerShell or Command Prompt:

Verify PyEnv Installation: Open PowerShell or Command Prompt and type:

pyenv --version

Step 3: Installing Poetry

Poetry is a dependency management and packaging tool for Python. It helps to manage dependencies, virtual environments, and packaging.

Install Poetry: Open PowerShell or Command Prompt and run

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -

Update Environment Variables: Add Poetry to your system PATH. The default installation path is usually:

C:\Users\<Your-Username>\AppData\Roaming\Python\Scripts

Verify Poetry Installation: Open a new PowerShell or Command Prompt window and type:

poetry --version

Step 4: Setting Up a Python Project with PyEnv and Poetry

Choose Python Version with PyEnv:

List available Python versions:

pyenv install --list

Install the desired Python version:

pyenv install <version>

Set the local Python version for your project:

pyenv local <version>

Create a New Python Project with Poetry:

Navigate to your desired project directory and create a new project:

poetry new my-project
cd my-project

Initialize a new Poetry project in an existing directory:

poetry init

Add Dependencies with Poetry:

Add a package:

poetry add <package-name>

Add development dependencies:

poetry add --dev <package-name>

Activate the Virtual Environment: Poetry automatically creates a virtual environment for your project. To activate it:

poetry shell

Run Your Python Code: You can now run your Python scripts within this environment:

python script.py

Conclusion

By following these steps, you have set up a powerful Python development environment on Windows using Python, PyEnv, and Poetry. This setup allows you to manage multiple Python versions effortlessly and handle project dependencies and virtual environments in a streamlined way. Happy coding!


Contact Us