docs/migrating.md
This guide provides step-by-step instructions for migrating to Pipenv from other Python dependency management tools. Whether you're coming from requirements.txt, pip, conda, or other tools, this guide will help you transition smoothly.
The requirements.txt file is a common way to specify Python dependencies. Pipenv provides a straightforward path to migrate from requirements.txt.
Navigate to your project directory:
$ cd your-project
Import your requirements.txt file:
$ pipenv install -r requirements.txt
This command:
Verify the generated Pipfile:
$ cat Pipfile
Generate a lock file:
$ pipenv lock
Test that everything works:
$ pipenv run python your_script.py
If you have separate requirements files for development and production:
Import production dependencies:
$ pipenv install -r requirements.txt
Import development dependencies:
$ pipenv install -r dev-requirements.txt --dev
For requirements files with complex constraints or comments:
Import the basic requirements:
$ pipenv install -r requirements.txt
Review the Pipfile and adjust as needed:
For packages with complex installation options, you may need to add them manually:
$ pipenv install package-name
Regenerate the lock file:
$ pipenv lock
# Start with a Django project using requirements.txt
$ cd django-project
# Import production requirements
$ pipenv install -r requirements.txt
# Import development requirements
$ pipenv install -r requirements-dev.txt --dev
# Review and adjust the Pipfile
$ nano Pipfile
# Generate the lock file
$ pipenv lock
# Test that Django works
$ pipenv run python manage.py runserver
If you've been using pip directly without requirements.txt files, you'll need to identify your dependencies first.
List installed packages:
$ pip freeze > requirements.txt
Review the requirements.txt file and remove packages that aren't direct dependencies of your project.
Follow the steps in the "Migrating from requirements.txt" section above.
If your environment has many packages and it's hard to identify direct dependencies:
Create a new Pipenv environment:
$ mkdir temp-project
$ cd temp-project
$ pipenv --python 3.x # Use your current Python version
Install your main dependencies one by one:
$ pipenv install django
$ pipenv install requests
# etc.
Install development dependencies:
$ pipenv install pytest --dev
$ pipenv install black --dev
# etc.
Copy the Pipfile and Pipfile.lock to your original project:
$ cp Pipfile Pipfile.lock /path/to/original/project/
$ cd /path/to/original/project/
$ pipenv install
If you're using virtualenv or venv with pip:
Activate your existing virtual environment:
$ source venv/bin/activate # or equivalent for your OS
Generate a requirements file:
$ pip freeze > requirements.txt
Deactivate the virtual environment:
$ deactivate
Initialize Pipenv and import requirements:
$ pipenv install -r requirements.txt
Optionally, remove the old virtual environment:
$ rm -rf venv/ # Use appropriate command for your OS
Poetry is another modern Python dependency management tool. Migrating from Poetry to Pipenv requires a few steps.
Export Poetry dependencies to requirements.txt:
$ poetry export -f requirements.txt --output requirements.txt
$ poetry export -f requirements.txt --dev --output dev-requirements.txt
Initialize Pipenv and import requirements:
$ pipenv install -r requirements.txt
$ pipenv install -r dev-requirements.txt --dev
Review the Pipfile and adjust as needed:
$ nano Pipfile
Generate the lock file:
$ pipenv lock
Some Poetry features don't have direct equivalents in Pipenv:
Scripts: Poetry's [tool.poetry.scripts] can be converted to Pipenv's [scripts] section.
Package building: If you're developing a library, you'll still need a setup.py or pyproject.toml for distribution.
Extra dependencies: Convert Poetry's extras to Pipenv's package options.
Poetry:
[tool.poetry.extras]
docs = ["sphinx"]
Pipenv:
[packages]
your-package = {path = ".", extras = ["docs"]}
Conda is a package manager that handles both Python and non-Python packages. Migrating from Conda to Pipenv requires some additional steps.
Export Conda environment to a file:
$ conda env export --from-history > environment.yml
Extract Python packages from the environment file:
$ grep -v "prefix:" environment.yml | grep -v "^name:" > conda_env.yml
Convert Conda packages to pip requirements (you may need to do this manually):
# Create a requirements.txt file with Python packages from conda_env.yml
Initialize Pipenv and import requirements:
$ pipenv install -r requirements.txt
Conda often manages non-Python dependencies that Pipenv can't handle. For these:
Document the non-Python dependencies separately.
Consider using Docker to manage system-level dependencies.
For development, you might need to install these dependencies using your system package manager.
If you're using pipenv-setup to maintain both Pipfile and setup.py:
Keep your setup.py or convert it to pyproject.toml.
Use Pipenv for development dependencies and environment management.
Continue to use pip/setuptools for package distribution.
After migrating to Pipenv, follow these best practices:
Remove old dependency management files that are no longer needed:
$ rm -f requirements.txt dev-requirements.txt requirements-dev.txt
Keep setup.py or pyproject.toml if you're developing a library.
Update your project documentation to reflect the new workflow:
Update your continuous integration and deployment pipelines:
# Example GitHub Actions workflow
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install pipenv
run: pip install pipenv
- name: Install dependencies
run: pipenv install --dev
- name: Run tests
run: pipenv run pytest
Ensure both files are committed to version control:
$ git add Pipfile Pipfile.lock
$ git commit -m "Migrate to Pipenv"
After migration, review your dependencies:
Remove unnecessary dependencies:
$ pipenv uninstall unnecessary-package
Update outdated packages:
$ pipenv update --outdated
$ pipenv update
Check for security vulnerabilities:
$ pipenv scan
Problem: Pipenv can't resolve dependencies due to conflicts.
Solution:
$ pipenv graph
$ pipenv lock --clear
Problem: Packages with C extensions fail to install due to missing system dependencies.
Solution:
# Ubuntu/Debian
$ sudo apt-get install build-essential python3-dev
# macOS
$ xcode-select --install
Problem: Pipenv resolves to different package versions than your previous tool.
Solution:
[packages]
requests = "==2.28.1"
pipenv install --ignore-pipfile to install exactly what's in the lock file.Problem: Can't access private repositories.
Solution:
[[source]]
name = "private"
url = "https://private-repo.example.com/simple"
verify_ssl = true
$ export PIP_EXTRA_INDEX_URL=https://user:[email protected]/simple
Use this checklist to ensure a successful migration:
Migrating to Pipenv provides several benefits:
While the migration process requires some effort, the long-term benefits make it worthwhile for most Python projects.
If you encounter issues during migration, refer to the Troubleshooting guide or seek help from the Pipenv community.