Original link: https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv
Translated by: Jiong
At Thoughtbot, we work with Ruby and rails, but often we always try to use the most appropriate language or framework to solve the problem. I've been exploring machine learning techniques Recently, so Python uses more.
A big difference between the Ruby project and the Python project processing is the difference in how you manage dependencies. There is currently nothing like bundler or gemfiles in the Python language, so Python developers typically use virtualenv to create a virtual environment and then create a dependency package list requirements.txt, and then they can use the Pip for installation.
This method generally works fine, but sometimes it shows some weird behavior, so you have to manually install or remove certain versions of the package, and remember to regularly update the Requirements.txt file to keep the project environment consistent. Especially if you want to install a Python package in your virtual environment, it's not necessarily associated with the project itself. In addition, some projects sometimes retain two versions of the Requirements.txt file-one for the development environment and one for the production environment, which can lead to more complexity.
Fortunately, Kenneth Reitz's newest tool pipenv can be used to simplify the management of dependencies in Python projects. It brings together the features of Pip,pipfile and Virtualenv and is a powerful command-line tool.
Entry
First use PIP to install pipenv and its dependencies,
Then change the directory to the folder that contains your Python project and start Pipenv,
- 1 CD My_project
- 2 pipenv Install
This will create two new files in the project directory Pipfile and Pipfile.lock, and if the project does not exist, create a new virtual environment for the project. If you add the –two or –three flag to the last command above, it initializes your project using Python 2 or three, respectively. Otherwise, the default version of Python will be used.
Managing Python Dependencies
Pipfile contains information about the dependencies of the project and supersedes the Requirements.txt files that are commonly used in Python projects. If you started pipenv in a project that has a requirements.txt file, you should use Pipenv to install all the dependent packages listed in the file before you remove it from the project.
To install a Python package for your project, use the Install keyword. For example
- Pipenv Install Beautifulsoup4
The current version of the beautiful soup package will be installed. You can use the Uninstall keyword to delete a package in a similar way,
- Pipenv Uninstall BEAUTIFULSOUP4
You can freeze the package name and its version, as well as a list of its dependencies, by updating Pipfile.lock. This can be done using the Lock keyword,
If another user clones the repository, you can add pipfiles to your git repository so they only need to install pipenv on their system, and then type,
Pipenv will automatically find the Pipfiles, create a new virtual environment and install the necessary packages.
Manage your development environment
Typically, there are python packages that are only needed in your development environment, not in your production environment, such as unit test packages. Pipenv will use the –DEV flag to keep the two environments separate.
- Pipenv Install --Dev nose2
Nose2, but it is also associated with packages that are only needed in the development environment. This is useful because now if you want to install your project in your production environment,
The NOSE2 package is not installed by default. However, if another developer clones your project into their own development environment, they can use the –DEV flag,
and install all dependencies, including development packages.
Run your code
To activate the virtual environment associated with your Python project, you can use simple shell commands, such as
You will run the which Python command in your virtual environment and display the path to the Python executable associated with your virtual environment. This is an easy way to run your own Python code in a virtual environment,
- Pipenv run Python my_project. Py
If you don't want to enter this much every time you run Python, you can set an alias in the shell, for example,
- Alias prp="pipenv run Python"
Note: This is reproduced in the blog, only as their own collection, convenient access to use.
How to use pipenv to manage your Python project