Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pipenv

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to use Pipenv, I believe most people still do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

pipenv is a project by the requests author that integrates virtualenv, pip, pipfile to make it easier to set up virtual environments for projects and manage third-party modules in virtual environments.

Install Pipenv

Pipenv installation and ordinary third-party library is no different, you can directly use the pip command to install.

pip install pipenv

On Mac, you can install it using the brew command.

brew install pipenv

Next, how to use Pipenv. When we start a new project, the first thing to do is to create a separate virtual environment for it (make sure you only have one Python version installed on your computer, otherwise unpredictable problems will always occur).

create a virtual environment

Virtual environments are Python execution environments that are used independently by each project.

#Enter the directory where the project is located cd your_projectpipenv install

Strictly speaking, pipenv does not directly provide the command to create a virtual environment, but silently creates it for you behind the scenes. Install is actually the command to install a third-party package. For new projects, it will first create a virtual environment, and at the same time, create an empty file named Pipfile in the project directory.

The name of the virtual environment is related to the project name. By default, Pipenv automatically selects the storage location of the virtual environment for you. Under Windows, it is usually C:\Users\\.virtualenvs.

Install third-party packages

Before installing packages using pip command, now you can use pipenv command to install, for example, I install the requests package:

pipenv install requests

What are the benefits of installing third party packages using pipenv? You don't need to activate the virtual environment before installation, you download it directly to the virtual environment, if you install it with pip, you also need to activate the environment first. (I often forget what names I give virtual environments)

After installing the package, Pipfile will be updated and Pipfile.lock will be added.

Structure in Pipfile file:

[[source]]name = "pypi"url = "https://pypi.org/simple"verify_ssl = true[dev-packages][packages]requests = "*"[requires]python_version = "3.7"

There are three things recorded in Pipfile:

Where did the package come from?

Which packages need to be downloaded, * means always download the latest version, dev-packages means packages dedicated to development use.

How many Python versions?

The Pipfile.lock file details these packages, including what hash value the package has and what other packages the package depends on.

The advantage of writing this information in Pipfile is that when we deploy the project to the formal environment, we no longer need to install it one by one. Directly executing pipenv install will automatically create a virtual environment and install all the packages in Pipfile.

Activate virtual environment

We still need to activate the virtual environment before running the program. We can use pipenv shell to activate it. We don't need to specify a name. Just execute the command under the current project.

pipenv shellLaunching subshell in virtual environment…Microsoft Windows [Version 10.0.18290.1000](c) 2018 Microsoft Corporation. All rights reserved. (weihub-flask-j3rUnHkH) D:\my\weihub-flask>

Did you notice that? The directory is preceded by a parenthesized string, which is the name of the virtual environment we just created. After activating the virtual environment, you can start running your program.

(weihub-flask-j3rUnHkH) D:\my\weihub-flask> python>>> import requests>>> requests.get("http://baidu.com").text'\n\n\n'>>>

Exit the virtual environment using the exit command, at which point the brackets disappear.

pipenv run

In addition, pipenv also prepares a run command that can execute commands in a virtual environment without you having to show the activated virtual environment, such as:

pipenv run python -m http.serverpipenv run python main.py

That's all you need to know about Pipenv. If you're interested, you can also learn about the following.

Import from requirements.txt

If it is an old project, you put all the dependent packages in requirements.txt, pipenv is very considerate, it supports installation from requirements.txt file, convenient for users to use pip smooth migration.

pipenv install -r requirements.txt

Install to development environment only

In the development environment, we need to install packages such as django-debug-toolbar and pytest. These packages are actually not used in the production environment, so there is no need to install them. When installing a package, we can add a parameter--dev after isntall to indicate that it is only installed in the development environment. For example:

pipenv install pytest --dev ```Execution in production: ``python#production pipenv install

Development Environment Execution:

#development environment pipenv install --dev

unload package

pipenv uninstall requests

Delete Virtual Environment

pipenv --rm

After deleting the virtual environment, the virtual environment directory and all dependent packages in it will be deleted, but Pipfile and Pipfile.lock files will remain.

Frequently Asked Questions:

If you fail to create a virtual environment using pipenv, an error like this appears:

pipenv installCreating a virtualenv for this project…Pipfile: D:\my\weihub-flask\PipfileUsing c:\users\lzjun\appdata\local\programs\python\python37-32\python.exe (3.7.1) to create virtualenv…[ ===] Creating virtual environment... Already using interpreter c:\users\lzjun\appdata\local\programs\python\python37-32\python.exeUsing base prefix 'c:\\users\\lzjun\\appdata\\local\\programs\\python\\python37-32'New python executable in C:\Users\lzjun\.virtualenvs\weihub-flask-j3rUnHkH\Scripts\python.exeERROR: The executable C:\Users\lzjun\.virtualenvs\weihub-flask-j3rUnHkH\Scripts\python.exe is not functioningERROR: It thinks sys.prefix is 'd:\\my\\weihub-flask' (should be 'c:\\users\\lzjun\\.virtualenvs\\weihub-flask-j3runhkh')ERROR: virtualenv is not compatible with this system or executableNote: some Windows users have reported this error when they installed Python for "Only this user" or have multiple versions of Python installed. Copying the appropriate PythonXX.dll to the virtualenv Scripts/ directory may fix this problem.Failed creating virtual environment[pipenv.exceptions.VirtualenvCreationException]: File "c:\users\lzjun\appdata\local\programs\python\python37-32\lib\site-packages\pipenv\cli\command.py", line 254, in install[pipenv.exceptions.VirtualenvCreationException]: editable_packages=state.installstate.editables,[pipenv.exceptions.VirtualenvCreationException]: File "c:\users\lzjun\appdata\local\programs\python\python37-32\lib\site-packages\pipenv\core.py", line 1741, in do_install[pipenv.exceptions.VirtualenvCreationException]: pypi_mirror=pypi_mirror,[pipenv.exceptions.VirtualenvCreationException]: File "c:\users\lzjun\appdata\local\programs\python\python37-32\lib\site-packages\pipenv\core.py", line 574, in ensure_project[pipenv.exceptions.VirtualenvCreationException]: pypi_mirror=pypi_mirror,[pipenv.exceptions.VirtualenvCreationException]: File "c:\users\lzjun\appdata\local\programs\python\python37-32\lib\site-packages\pipenv\core.py", line 506, in ensure_virtualenv[pipenv.exceptions.VirtualenvCreationException]: python=python, site_packages=site_packages, pypi_mirror=pypi_mirror[pipenv.exceptions.VirtualenvCreationException]: File "c:\users\lzjun\appdata\local\programs\python\python37-32\lib\site-packages\pipenv\core.py", line 935, in do_create_virtualenv[pipenv.exceptions.VirtualenvCreationException]: extra=[crayons.blue("{0}".format(c.err)),][pipenv.exceptions.VirtualenvCreationException]: Fatal Python error: initfsencoding: unable to load the file system codecTraceback (most recent call last): File "C:\Users\lzjun\.virtualenvs\weihub-flask-j3rUnHkH\lib\encodings\__init__.py", line 31, in File "C:\Users\lzjun\.virtualenvs\weihub-flask-j3rUnHkH\lib\codecs.py", line 481 except UnicodeDecodeError, exc: ^SyntaxError: invalid syntax

It is likely that there are multiple versions of Python installed on your system, you can fuzzy search Python*.dll, delete all those irrelevant, and try again.

How to Increase Download Speed

Modify the URL in Pipfile file to specify the download source as Alibaba Cloud or Douban and other faster domestic sources.

[[source]]name = "pypi"url = "https://mirrors.aliyun.com/pypi/simple"#or Douban source url = "http://pypi.douban.com/simple"verify_ssl = true

What are Pipenv's weaknesses?

Slow, sometimes it feels very slow, it has been stuck in the Locking process, you skip the lock link when installing, use the command

pipenv install --skip-lock

For more instructions, please refer to the official documentation: https://pipenv.readthedocs.io/en/latest/basics/

The above is all the content of this article "How to use Pipenv", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report