Python - Environment Setup with Pipx, Poetry, and Pyenv ✨
This guide will walk you through setting up your Python environment using pipx, poetry, and pyenv. These tools work together seamlessly to help you manage isolated environments, dependencies, and Python versions efficiently. Ready to streamline your Python setup? Let’s get started! 💪
Overview
- Pyenv: Keeps multiple versions of Python tidy and under control.
- Pipx: Lets you install Python applications in their own isolated environments.
- Poetry: Manages your project dependencies and sets up virtual environments like a pro.
Together, they make your Python setup clean, powerful, and just a bit magical. ✨
Prerequisites
Before we dive in, make sure you have the essentials:
- Python: A system version installed, just as a fallback.
- Homebrew (for macOS): Handy for managing dependencies.
# Update Homebrew to stay current
brew update
# Install Pyenv to manage Python versions
brew install pyenv
# Install Pipx to manage standalone Python tools
brew install pipx
# Install Poetry to manage your projects
brew install poetry
Setting Up Python with Pyenv
-
Install Python using Pyenv:
pyenv install 3.11.9 pyenv global 3.11.9This installs Python 3.11.9 and sets it as your global version. 🎉 You can verify the installation with:
pyenv versionsThis should list Python 3.11.9 as your active version.
-
Configure Your Shell for Pyenv:
Add the following to your
.bash_profileor.zshrc:export PATH="$(pyenv root)/shims:$PATH" eval "$(pyenv init --path)"Then reload your shell:
source ~/.zshrcTo confirm everything is set up correctly, run:
pyenv which pythonThis should point to the path where Python 3.11.9 is installed. Now you’re all set to use Pyenv smoothly in your terminal! 😎
Installing Pipx and Poetry
-
Install Pipx to ensure it’s correctly set up in your path:
pipx ensurepathYou can verify the installation by checking the version:
pipx --version -
Install Poetry via Pipx:
pipx install poetryInstalling Poetry with Pipx keeps it neatly separated from your global Python packages—clean and organized, just how it should be. 🧼 Verify the installation:
poetry --version
Configuring Poetry
Let’s configure Poetry to ensure the virtual environment setup is optimized for your development.
-
Make Virtual Environments Local to Each Project:
poetry config virtualenvs.in-project true- This setting tells Poetry to create the virtual environment inside the project directory (in a
.venvfolder). - By default, Poetry creates virtual environments in a centralized location (
~/Library/Caches/pypoetry/virtualenvson macOS), but creating it in the project is often more convenient. - Benefits of doing this:
- Your project is self-contained—easy to zip, share, and move.
- Finding the virtual environment is simple.
- IDEs like VSCode integrate perfectly with this setup.
- Everything is easier to track in version control.
- Need to clean up? Just delete the whole project folder!
You can verify this configuration:
poetry config virtualenvs.in-projectThe output should be
true. - This setting tells Poetry to create the virtual environment inside the project directory (in a
-
Boost Installation Speed with More Workers:
poetry config installer.max-workers 8- This sets the number of parallel workers Poetry uses when installing packages.
- The default is usually 4 workers, but using 8 can provide a significant boost, especially on multi-core systems.
- This is particularly great for heavier packages like TensorFlow and PyTorch.
- Note: The optimal number depends on your CPU:
- M3: 8 workers is ideal.
- M3 Pro: 8-12 workers are recommended.
- M3 Max: Up to 16 workers for best results.
Verify this setting with:
poetry config installer.max-workersThe output should reflect the number of workers you’ve configured.
Verifying Poetry Configuration
To verify your current Poetry settings:
poetry config --list
This command will list all your current Poetry configurations, including the ones you just added. Make sure everything’s set just the way you like it! 🛠️
Creating a New Project with Poetry
To create a new project and verify the setup:
-
Create a New Poetry Project:
poetry new my_projectThis will create a new folder named
my_projectwith a basic Python project structure. -
Navigate to Your Project and Install Dependencies:
cd my_project poetry installThis will create a
.venvfolder insidemy_projectand install the default dependencies. -
Activate the Virtual Environment:
poetry shellYou should now be in the virtual environment for
my_project. You can verify this by checking the Python path:which pythonIt should point to the
.venvfolder inside your project directory.
Unsetting Poetry Configuration
If you need to remove these settings at any point:
poetry config virtualenvs.in-project --unset
poetry config installer.max-workers --unset
This will revert the configurations to their default values. ✨
Summary
- Pyenv helps you manage different Python versions effortlessly.
- Pipx keeps your Python applications tidy and isolated.
- Poetry handles project dependencies and ensures everything is organized in virtual environments.
- Setting Poetry to create virtual environments in the project folder (
.venv) makes your projects more portable and easier to manage. - Adjusting
installer.max-workersuses your CPU efficiently, making installations faster and smoother.
With this setup, your workflow will be cleaner, more efficient, and—dare we say it—a lot more enjoyable. Happy coding! 🚀✨