安装 Pipenv
Pipenv 是一个生产流的工具,旨在将最好的打包世界( bundler, composer, npm, cargo, yarn, etc. )带入 Python 世界。它将 Pipfile、pip 和 virtualenv 整合到一个命令中。
我们可以使用 pip 命令安装 pip3 install --user pipenv
:
$ pip3 install --user pipenv
Collecting pipenv
Downloading pipenv-2022.5.2-py2.py3-none-any.whl (3.9 MB)
|████████████████████████████████| 3.9 MB 634 kB/s
Collecting virtualenv-clone>=0.2.5
Downloading virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from pipenv) (2019.11.28)
Collecting pip>=22.0.4
Downloading pip-22.1.2-py3-none-any.whl (2.1 MB)
|████████████████████████████████| 2.1 MB 2.3 MB/s
Requirement already satisfied: setuptools>=36.2.1 in /usr/lib/python3/dist-packages (from pipenv) (45.2.0)
Requirement already satisfied: virtualenv in /usr/lib/python3/dist-packages (from pipenv) (20.0.17)
Installing collected packages: virtualenv-clone, pip, pipenv
Successfully installed pip-22.1.2 pipenv-2022.5.2 virtualenv-clone-0.5.7
复制代码
为什么推荐使用 pipenv,因为它会自动为您的项目创建和管理虚拟环境 virtualenv,并在您安装/卸载包时从您的 Pipfile 中添加/删除包。它还生成非常重要的 Pipfile.lock,用于生成确定性构建。
安装 Django
创建一个项目文件夹 mytodo
:
$ mkdir mytodo
$ cd mytodo
复制代码
Django 是一个高级 Python Web 框架,它鼓励快速开发和简洁、实用的设计。它由经验丰富的开发人员构建,解决了 Web 开发的大部分麻烦,因此您可以专注于编写应用程序,而无需重新发明轮子。它是免费和开源的。
使用 pipenv install django
在虚拟环境中安装您的 django 框架:
$ sudo pipenv install django
Creating a virtualenv for this project...
Pipfile: /home/wade/PythonProject/mytodo/Pipfile
Using /usr/bin/python3 (3.8.10) to create virtualenv...
⠼ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 439ms
creator CPython3Posix(dest=/root/.local/share/virtualenvs/mytodo--AJz7sim, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, pkg_resources=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1.debian.1)
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/mytodo--AJz7sim
Creating a Pipfile for this project...
Installing django...
Adding django to Pipfile's [packages]...
✔ Installation Succeeded
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (a6086c)!
Installing dependencies from Pipfile.lock (a6086c)...
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
复制代码
验证安装
使用 pipenv run python3
激活 python 环境
导入 django 包: import django
打印 django 版本:print(django.get_version())
验证 Django 安装成功:
$ sudo pipenv run python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
4.0.5
>>>
复制代码
启动 django 程序
使用 pipenv run python3 manage.py runserver
来启动默认的 django 程序:
$ sudo pipenv run python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 07, 2022 - 07:54:34
Django version 4.0.5, using settings 'todobackend.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[07/Jun/2022 07:55:05] "GET / HTTP/1.1" 200 10697
[07/Jun/2022 07:55:07] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[07/Jun/2022 07:55:16] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[07/Jun/2022 07:55:16] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[07/Jun/2022 07:55:16] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[07/Jun/2022 07:55:18] "GET /favicon.ico HTTP/1.1" 404 2115
复制代码
访问本地服务器地址 http://127.0.0.1:8000/
,即可看到如下界面,django 程序启动成功:
总结
本文先介绍了一个非常好用的 Python 项目环境与依赖管理工具 pipenv,然后进行本地环境安装 pipenv ,通过 pipenv 进行 django 项目的管理,最后介绍了 django 虚拟环境中安装到启动一个 django 程序。
参考链接:
评论