发布于 2026-01-06 0 阅读
0

Python:如何正确设置 Virtualenv

Python:如何正确设置 Virtualenv

原文发布于我的博客

介绍

Pipenv是一个用于 Python 项目的依赖管理工具。如果您熟悉 Node.js 的 npm 或 Ruby 的 bundler,那么它的理念与这些工具类似。

每次你想创建一个新的Python项目或者学习一门新课程时,都会被VirtualEnv搞得晕头转向?
VirtualEnv到底是什么?
如何正确配置它?

要求

Python:3.4 或更高版本
pip

包管理器与依赖管理器

基于StackOverflow 的一个回答

软件包管理器- 用于配置系统,即设置开发环境,通过这些设置,您可以构建许多项目。

依赖项管理器——专用于特定项目。您可以管理单个项目的所有依赖项,这些依赖项将保存在您的项目中。当您启动另一个项目时,需要重新管理您的依赖项。

我们为什么需要它

使用依赖管理器的主要目的是分离应用程序依赖项,这样就可以在不同的项目中使用同一个框架的不同版本。

一个简单的使用案例:

假设我们有两个 Django 应用程序,并且我们想要安装不同版本的 Django。

我们做什么?

我们可以选择在一台机器上安装所有版本,但这并不是理想的做法。

如何正确设置虚拟环境

安装

作为 Python 程序员,我们有很多选择。

  • 皮普
  • Pipenv
  • ETC...

本教程将使用pipenv ,我认为它是最容易配置的工具之一
,推荐用于协作项目和团队项目。

请确保您的计算机上已安装 Python 和 pip。

让我们检查一下您的安装情况。

$ python --version
Enter fullscreen mode Exit fullscreen mode

输出结果应该类似于这样

Python 3.7.5
Enter fullscreen mode Exit fullscreen mode

检查 pip 安装情况

$ pip --version
Enter fullscreen mode Exit fullscreen mode

输出

pip 19.3.1 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7
Enter fullscreen mode Exit fullscreen mode

使用pip安装pipenv

$ pip install --user pipenv
Enter fullscreen mode Exit fullscreen mode

检查安装情况

$ pipenv --version
Enter fullscreen mode Exit fullscreen mode

输出

pipenv, version 11.9.0
Enter fullscreen mode Exit fullscreen mode

创建新项目

$ mkdir test_pipen && cd test_pipenv
$ touch app.py
Enter fullscreen mode Exit fullscreen mode

正在为您的项目安装软件包

$ pipenv install requests
Enter fullscreen mode Exit fullscreen mode
Creating a virtualenv for this project…
Using /usr/bin/python3 (3.7.5) to create virtualenv…
⠋Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy/bin/python3
Also creating executable in /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy/bin/python
Installing setuptools, pip, wheel...
done.

Virtualenv location: /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy
Creating a Pipfile for this project…
Installing requests…
Looking in indexes: https://pypi.python.org/simple
Collecting requests
 Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5
 Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
 Using cached https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2
 Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17
 Using cached https://files.pythonhosted.org/packages/b9/63/df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99/certifi-2019.11.28-py2.py3-none-any.whl
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.7

Adding requests to Pipfile's [packages]…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (5a8f8c)!
Installing dependencies from Pipfile.lock (5a8f8c)…
 🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01

Enter fullscreen mode Exit fullscreen mode

激活您的新环境

$ pipenv shell
$ (test_pipenv-3gXMtvzy) ....
Enter fullscreen mode Exit fullscreen mode

在你的 app.py 文件中添加这行代码

import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))
Enter fullscreen mode Exit fullscreen mode

运行应用程序

$ python app.py
Enter fullscreen mode Exit fullscreen mode

你应该会看到类似这样的输出:

Your IP is 0.0.0.224, 0.0.0.224
Enter fullscreen mode Exit fullscreen mode

下次见 :-)

文章来源:https://dev.to/xarala221/python-how-to-setup-your-virtualenv- Correctly-4l1