Installation and use of pipenv
reference resources:
Getting started with Pipenv - brief book (jianshu.com)
introduce
Python's virtual environment management tools: the most commonly used are virtualenv, virtualenvwrapper and pipenv. The management of these virtual environments is based on virtualenv, but different packages are made to achieve better results.
- Virtualenv, this tool will create a folder called virtualenv under the project directory_ Name, this directory will contain python copies, and then all dependencies will be saved to this directory. When the virtual environment is activated, you can install the required dependency packages, and the installed dependency packages will be saved to the project virtual environment directory virtualenv_name will not pollute the global environment of the system;
- Disadvantages:
- You may need to manually install / remove certain versions of the package
- Regularly update requirements Txt to maintain the consistency of the project environment
- Two versions of requirements are retained Txt, one for development and one for production environment, resulting in more complexity
- Disadvantages:
- virtualenvwrapper is the encapsulation of virtualenv interface. The virtual env wrapper will save the directories of the virtual environment uniformly without manual management, which is more convenient to use;
- Pipenv, a Python dependency management tool released by Kenneth Reitz in January 2017, is now maintained by PyPA. You can think of it as a combination of pip and virtualenv, and the Pipfile based on it is used to replace the old dependent recording method (requirements.txt). Pipenv is much simpler in ease of use, and the lock file is added to better lock the version. If there are no special requirements, you can directly use the lock version of pipenv, and the development can iterate in small steps to realize the steady upgrading of dependencies.
- Realize the simultaneous management of python virtual environment and related package dependencies in the project
Advantages of pipenv
- pipenv will create Pipfile and Pipfile in the project directory The lock file manages the dependencies between packages. Previously, we needed to export the virtual environment dependency package as requirements Txt. Once the dependent package changes, it needs to be re exported. Now Pipfile and Pipfile Lock file can save these steps and be more convenient to manage;
- The installation and uninstallation package does not need to activate the virtual environment and operates directly under the project folder;
- When uninstalling, automatically check whether the dependent library is dependent on other packages to select whether to delete it completely.
- There is no need to activate the virtual environment to execute code, as long as there is a pipfile file through pipenv run Python XX py.
- It is convenient for docker container management. Pipfile supports the generation of requirements files to facilitate docker management of project code. In addition, pipfile also supports - dev environment. Many debugging tools can be installed during debugging without affecting the environment of production environment.
- Hash verification is used in various places. No matter installing or uninstalling the package, it is very safe, and security vulnerabilities will be automatically exposed.
- By loading env file simplifies development workflow
install
Anaconda Prompt or cmd input - > PIP install pipenv
Check whether the installation is successful: pipenv --version
Preview the usage of pipenv --help
Create environment
-
Create environment pipenv install & pipenv shell [directly enter cmd or pipenv shell in the address line]
Using pipenv install & pipenv shell
pipenv install needs to be operated in the DOS interface, and the virtual environment name is displayed in front of the command prompt before entering the environment
pip shell can input directly in the address line, activate the pipenv tool of anaconda, and enter the virtual environment in one step. It may not be used without anaconda
(WeiBoImageCrawling-rDB0ue93) F:\Ccode\WeiBoImageCrawling>pipenv install Creating a virtualenv for this project... Pipfile: F:\Ccode\WeiBoImageCrawling\Pipfile Using C:/Users/Fleeting years/AppData/Local/Programs/Python/Python37-32/python.exe (3.7.4) to create virtualenv... [ ] Creating virtual environment...created virtual environment CPython3.7.4.final.0-32 in 2272ms creator CPython3Windows(dest=C:\Users\Fleeting years\.virtualenvs\WeiBoImageCrawling-rDB0ue93, clear=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Fleeting years\A ppData\Local\pypa\virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.36.2 activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator Successfully created virtual environment! Virtualenv location: C:\Users\Fleeting years\.virtualenvs\WeiBoImageCrawling-rDB0ue93 Installing dependencies from Pipfile.lock (a65489)... ================================ 0/0 - 00:00:00
-
The above command generates Pipfile and Pipfile Lock, automatically generate a random virtual environment directory name [project name + random characters].
-
Pipfile: used to save the python version, dependent package and other related information of the project
Pipfile files can be moved to other projects separately for the establishment of project virtual environment and the installation of dependent packages
-
Pipfile.lock: used to lock pipfile
-
-
pipenv install
Pipfile file
-
Exists in the directory, and the virtual environment is generated according to this Pipfile.
-
If it does not exist in the directory, this command will create a Pipfile file.
If the virtual environment in the project directory has been created and there is a Pipfile file, the dependent package will be installed according to the Pipfile file
-
-
Execute the command under windows, and the generated virtual environment is in C:\Users \ user name Under the virtualenvs folder
-
General virtual environment directory name: the project directory name where the environment is created + random string, eg: myblog-Gtn4e1q9
Parsing pipfile files
[[source]] name = "pypi" url = "https://pypi.org/simple" verify_ssl = true [dev-packages] [packages] [requires] python_version = "3.7" [scripts] test = "python3 -m unittest discover -s ./tests" dev = "python3 manage.py runserver 0.0.0.0:8000"
-
source is used to set the warehouse address, that is, where to download the packages required by the virtual environment
- You can manually modify the url to the source you want to use
-
Packages is used to specify the packages that the project depends on. It can be used in the production environment and generate requirements files
-
Dev packages is used to specify the packages required by the development environment. Such packages are only used in the development process, not the packages related to the production environment, such as unit testing. They are only useful in the development stage, so they are separated and easy to manage. Install debugging tools, performance testing tools and python syntax tools
-
Specify the target Python version in requires
-
[scripts] add custom script commands and execute the corresponding commands in the virtual environment through pipenv run:
pipenv run test is equivalent to executing pipenv run python3 - M unittest discover - S/ tests
pipenv run dev is equivalent to pipenv run python3 manage py runserver 0.0.0.0:8000
Parse pipfile Look file
-
Pipfile.lock records the version number and hash of the dependencies installed in the current virtual environment to ensure that the dependencies installed according to these values are consistent every time. This file is used to ensure the integrity of the package. Remember, under no circumstances should you manually modify this file!
-
When the project is submitted, the Pipfile file and Pipfile The lock file is submitted to other developers for cloning and downloading. They can run the command pipenv install to generate their own virtual environment according to the Pipfile file.
Specifies the Python version of the virtual environment
pipenv --python 3.6 #Specifies to use Python 3 6 virtual environment pipenv --two #Use Python 2 of the system to create a virtual environment pipenv --three #Use Python 3 of the system to create a virtual environment #Note: the above three parameters can only be used separately. They are also destructive, deleting the current virtual environment and replacing it with the appropriate version of the virtual environment.
- When a python version is given, Pipenv will automatically scan the Python interpreter in the system that can match the given Python version. If no version is specified, the system default Python version will be used.
Running code in a virtual environment
-
Activate the environment pipenv shell
-
After creating the environment, you will automatically enter the virtual environment. When you exit the virtual environment and re-enter the virtual environment, you need to execute the above commands
-
After executing the command, the name of the virtual environment such as * * (TestPipenv-tmf99lpc) * * appears in front of the command prompt, indicating that you have entered the virtual environment
-
The python version, pip and third-party libraries in the virtual environment will not conflict with the libraries in the system
-
-
Run the code project Python XXX in a virtual environment py eg:
(WeiBoImageCrawling-rDB0ue93) F:\Ccode\WeiBoImageCrawling>python xxx.py xxx.py function...
-
To run code in a virtual environment without entering the virtual environment, you can use pipenv run Python XXX Py command, eg:
F:\Ccode\WeiBoImageCrawling>pipenv run python xxx.py xxx.py function...
-
pipenv management Python package
-
Install dependent packages to the virtual environment
- pipenv install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/
- pipenv install parsel==1.3.1 -i https://pypi.tuna.tsinghua.edu.cn/simple/
- Install the nose2 package and associate it as a package that is only needed in the development environment
- pipenv install --dev nose2 -i https://pypi.tuna.tsinghua.edu.cn/simple/
--Dev flag. The package with this flag is only used in the development environment, and the installation package is recorded in the dev package in Pipfile
After cloning the Profile file, use the command pipenv install to create a virtual environment. By default, the installation package under dev package will not be installed unless you use the command pipenv install --dev
Pipefile and pipefile Lock will be automatically updated according to your operation. If you need to manually modify the package dependency conditions, you can manually edit the pipefile and install it.
Note: it is strongly recommended to wrap the package name and version number in double quotation marks to avoid unix Input and output redirection in operating system pipenv install requests # No version specified pipenv install requests==2.31 # Specify version pipenv install --dev requests # Install the test package in dev development environment (you can add the – skip lock parameter) pipenv install "requests>=1.4" # Only install versions equal to or greater than 1.4.0 pipenv install "requests<=2.13" # Only install versions less than or equal to 2.13.0 pipenv install "requests>2.19" # Install version 2.19.1 but not version 2.19.0 pipenv install "requests!=2.19" # Avoid installing a version pipenv --site-packages # Loading system installed Python package pipenv install package_name --skip-lock # Skip lock and update the hash values of all reports after the project is developed pipenv install -r path/to/requirements.txt #Import a requirements file pipenv install "requests~=2.2" # Lock the major version of the package (this is equivalent to using 2. *) Please use it first ~= Identifier instead of == Identifier because the latter blocks pipenv Update package After installation, the Pipfile See in file
-
Uninstall package
pipenv uninstall beautifulsoup4 #Uninstall beatifulsoup4 in the virtual environment where the project is located pipenv uninstall --all #Remove all installed packages from the virtual environment, but pipfile Lock files are not affected pipenv uninstall --all--dev #Uninstall all development packages from the virtual environment and remove them from the Pipfile file
-
Update command
pipenv update requests #Update requests package in project pipenv update #Update all packages in the project pipenv update --outdated #View which existing packages have expired
-
View command
pipenv --where #View project location pipenv --venv #View virtual environment location pipenv --py #View interpreter information pipenv graph #Display existing dependent packages / dependency structures of packages / installed modules pipenv list #View installed packages pipenv lock #Update pipfile Lock file locks the dependent version of the current environment exit #Exit virtual environment
-
Delete virtual environment pipenv --rm
- It is found in practice that the virtual environment is added or deleted many times in the same project directory, and the name of the virtual environment remains unchanged, or the memory space remains unchanged
-
Other commands pipenv -h
Options: --where Output project home information. --venv Output virtualenv information. --py Output Python interpreter information. --envs Output Environment Variable options. --rm Remove the virtualenv. --bare Minimal output.Minimum output? --completion Output completion (to be executed by the shell). --man Display manpage. Displays the man page. --support Output diagnostic information for use in GitHub issues.use GitHub When, output diagnostic information --site-packages / --no-site-packages Enable site-packages for the virtualenv. [env var: PIPENV_SITE_PACKAGES] --python TEXT Specify which version of Python virtualenv should use. --three / --two Use Python 3/2 when creating virtualenv. --clear Clears caches (pipenv, pip, and pip-tools). [env var: PIPENV_CLEAR] Clear cache -v, --verbose Verbose mode. Detailed mode --pypi-mirror TEXT Specify a PyPI mirror. appoint PyPI image --version Show the version and exit. -h, --help Show this message and exit. Usage Examples: Create a new project using Python 3.7, specifically: $ pipenv --python 3.7 Remove project virtualenv (inferred from current directory): $ pipenv --rm Install all dependencies for a project (including dev): $ pipenv install --dev Create a lockfile containing pre-releases:To create a lock file that contains a pre release version: $ pipenv lock --pre Show a graph of your installed dependencies: $ pipenv graph Check your installed dependencies for security vulnerabilities:Check installed dependencies for security vulnerabilities: $ pipenv check Install a local setup.py into your virtual environment/Pipfile:Local setup.py Install to virtual environment $ pipenv install -e . Use a lower-level pip command:Use lower level pip command $ pipenv run pip freeze Commands: check Checks for PyUp Safety security vulnerabilities and against PEP 508 markers provided in Pipfile. clean Uninstalls all packages not specified in Pipfile.lock. graph Displays currently-installed dependency graph information. install Installs provided packages and adds them to Pipfile, or (if no packages are given), installs all packages from Pipfile. lock Generates Pipfile.lock. open View a given module in your editor. run Spawns a command installed into the virtualenv. shell Spawns a shell within the virtualenv. sync Installs all packages specified in Pipfile.lock. uninstall Uninstalls a provided package and removes it from Pipfile. update Runs lock, then sync.
$ pipenv Usage: pipenv [OPTIONS] COMMAND [ARGS]... The command has the following options Options:pipenv --update to update Pipenv & pip --where Displays the path where the project file is located --venv Displays the path where the virtual environment's actual files are located --py Show virtual environment Python Interpreter path --envs Displays option variables for the virtual environment --rm Delete virtual environment --bare Minimize output --completion Full output --man Display help page --three / --two use Python 3/2 Create a virtual environment (note the installed on this machine) Python Version) --python TEXT Specify a Python Version as the installation source for the virtual environment --site-packages Attached installation original Python Third party libraries in the interpreter --jumbotron I don't know what.... --version Version information -h, --help Help information Available command parameters: Commands: check Check for security vulnerabilities graph Displays the current dependency graph information install Install virtual environment or third-party library lock Lock and generate Pipfile.lock file open View a library in the editor run Running commands in a virtual environment shell Enter virtual environment uninstall Uninstall a library update Uninstall all current packages and install their latest versions
pyinstaller package generation exe
-
Install pyinstaller
pipenv install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/
-
Packaging command
Pyinstaller - f < entry py>
-F Package into executable -w (a lowercase letter) Do not display the command line window
eg: pyinstaller -F E:/tf/lab.8.1-huffman-coding/byteSourceCoder.py
Compatible with virtualenv
-
pipenv can generate requirements using the same command as virtualenv Txt file
pipenv lock -r --dev > requirements.txt
-
Similarly, you can also use requirements. Net like virtualenv Txt file installation package
pipenv install -r requirements.txt
This command allows us to reuse the previous requirements Txt file to build our new development environment and smoothly move our project to pipenv.
pipenv install syntax is fully compatible with pip install
Basic use of pip
pip install <Package name> #After installation, it is automatically downloaded to the Lib > site packages directory of Python pip uninstall <Package name> #uninstall pip freeze #View installed packages and their versions pip list -o #View upgradeable packages pip install -U <Package name> #Upgrade specified package python -m pip install --upgrade pip #Upgrade pip