Pyenv, Pipenv
Pyenv
Allows to have multiple python versions.
Managing Multiple Python Versions on Arch Linux
To manage multiple Python versions on Arch Linux, we recommend using pyenv
. This tool allows you to easily install and switch between different Python versions.
Setting Up pyenv
on Arch Linux with zsh
- Install
pyenv
and dependencies:
sudo pacman -S --needed base-devel openssl zlib xz
curl https://pyenv.run | bash
- Configure
pyenv
forzsh
:
Add the following lines to your ~/.zshrc
file:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
- Apply the changes by restarting your shell or sourcing the configuration:
source ~/.zshrc
- Install the desired Python versions:
pyenv install 3.10.12
pyenv install 3.11.4
- Set a global Python version (optional):
pyenv global 3.10.12
- Set a Python version for a specific project:
cd your_project_directory
pyenv local 3.11.4
- Verify the current Python version:
python --version
By using pyenv
, you can manage multiple Python versions seamlessly on your Arch Linux system without impacting your global Python environment.
Pipenv
Introduction to Pipenv
Pipenv is a tool that aims to bring the best of all packaging worlds (bundling, dependency management, and virtual environments) to the Python world. It automatically creates and manages a virtual environment for your projects, as well as adds/removes packages from your Pipfile
as you install/uninstall packages. It also generates the Pipfile.lock
, which is used to produce deterministic builds.
Why Use Pipenv?
Simplified Dependency Management: Pipenv replaces the combination of
requirements.txt
and virtualenv with a single tool that handles both package dependencies and virtual environments.Environment Consistency: The
Pipfile.lock
ensures that all installations are reproducible, locking the exact versions of dependencies.Security: Pipenv checks for known security vulnerabilities in your dependencies.
User-Friendly: Pipenv provides a simple command-line interface that is easier to use than managing virtual environments and dependencies manually.
Installing Pipenv
Since you installed Pipenv using pacman -S python-pipenv
, it means you are on an Arch-based Linux distribution. If others need to install it, they can do so using the following:
pacman -S python-pipenv
Alternatively, Pipenv can be installed via pip:
pip install pipenv
Using Pipenv
Here is a step-by-step guide on how to use Pipenv for your Python projects.
1. Creating a New Project
Navigate to your project directory:
mkdir myproject
cd myproject
Create a new virtual environment and Pipfile
:
pipenv install
2. Installing Packages
To install a package and add it to your Pipfile
, use:
pipenv install <package_name>
For example, to install requests
:
pipenv install requests
To install a development-only package:
pipenv install --dev <package_name>
For example, to install flake8
for development:
pipenv install --dev flake8
3. Uninstalling Packages
To uninstall a package and remove it from your Pipfile
:
pipenv uninstall <package_name>
4. Activating the Virtual Environment
To activate the virtual environment, use:
pipenv shell
To exit the virtual environment, simply type exit
.
5. Running Commands in the Virtual Environment
You can run commands in the virtual environment without activating it:
pipenv run <command>
For example, to run a Python script:
pipenv run python myscript.py
6. Locking Dependencies
To generate a Pipfile.lock
:
pipenv lock
This file will lock the exact versions of your dependencies, ensuring consistent environments across different machines.
7. Checking for Security Vulnerabilities
Pipenv can check for known security vulnerabilities in your dependencies:
pipenv check
8. Listing Installed Packages
To list installed packages:
pipenv graph
This will display a dependency graph of installed packages.
Example Workflow
Here’s an example workflow for setting up a new project:
- Set up a new project:
mkdir myproject
cd myproject
pipenv install
- Install dependencies:
pipenv install requests
pipenv install --dev flake8
- Activate the virtual environment:
pipenv shell
- Run your Python script:
python myscript.py
- Exit the virtual environment:
exit
- Generate a
Pipfile.lock
:
pipenv lock
- Check for vulnerabilities:
pipenv check