Pyenv
What is Pyenv?
pyenv
is a tool that allows you to easily switch between multiple versions of Python on your machine. It manages Python installations and allows you to set global or project-specific Python versions.
Key Features of Pyenv:
- Multiple Python Versions: Install and manage multiple versions of Python on the same system.
- Global Version Management: Set a global Python version that is used system-wide.
- Local Version Management: Set a Python version for a specific project directory, which overrides the global version.
- Version Switching: Easily switch between different Python versions using simple commands.
When to Use Pyenv:
- Managing Multiple Python Versions: If you work on projects that require different versions of Python (e.g., one project requires Python 2.7 and another Python 3.10).
- Isolating Python Versions: To avoid conflicts between projects that need different Python versions.
When to Use
Use pyenv
when you need to switch between different versions of Python for different projects or want to ensure that you can run old or new Python code on your machine without conflicts.
Example
# 1. Install a Python Version
pyenv install 3.9.6
# 2. List All Installed Python Versions
pyenv versions
# 3. Set a Global Python Version
pyenv global 3.9.6
# 4. Set a Local Python Version for a Project
cd /path/to/project
pyenv local 3.8.10
# 5. Display the Current Active Python Version
pyenv version
# 6. Uninstall a Python Version
pyenv uninstall 3.7.9
# 7. Display the Path to the Active Python Executable
pyenv which python
# 8. Display Help for Pyenv Commands
pyenv help
# 9. List All Available Python Versions for Installation
pyenv install --list
# 10. Remove a Local Python Version for a Project
pyenv local --unset
Differences Between Pyenv and Pipenv
Feature | Pyenv | Pipenv |
---|---|---|
Purpose | Manage multiple Python versions | Manage project dependencies and virtual environments |
Manages | Python versions | Python packages and virtual environments |
Global vs Local | Can set global and local Python versions | Manages dependencies per project |
Virtual Environment | Does not create virtual environments | Automatically creates and manages virtual environments |
Dependency Management | No | Yes, via Pipfile and Pipfile.lock |
Info
In many cases, you might use both tools together: pyenv
to manage Python versions and pipenv
to manage project-specific dependencies within the chosen Python version.