首页 后端开发 Python教程 如何在 inutes 中创建自己的 Python 项目

如何在 inutes 中创建自己的 Python 项目

Dec 29, 2024 am 08:58 AM

为什么选择Python包?

Python支持所有类型的执行;您可以直接在 shell 中运行 Python 代码,或者将代码放入文件中并稍后运行。

有时候开始一个新的Python项目是非常困难的;写剧本?写一个模块?写个包吗?

最好的选择是微片模式:编写一个脚本,然后在模块中重写它,然后在包中重写。

这种模式让您不必每天重新发明轮子,并且可以在将来重用代码。

Python包结构

Python 包具有以下结构:

pkg
├── __init__.py
├── module1.py
└── subpkg
    ├── __init__.py
    ├── __main__.py
    └── module2.py
登录后复制
登录后复制

文件夹pkg是一个包,因为包含__init__.py模块。 subpkg 文件夹也是一个包;是pkg的子包。
module1.py 和 module2.py 是其包的模块。
__main__.py 模块允许执行包。

只有这里吗?其他的事情?

如果你要成为一名 Python 开发人员,那么你会使用其他常用工具。

按照顺序,您编写的每一段代码都遵循以下步骤:

  1. 将Python代码写入你的包
  2. 跟踪您的修改
  3. 测试您编写的所有代码
  4. 将您的代码放入您测试的环境中
  5. 将代码推送到远程存储库
  6. 构建您的分发包
  7. 将你的包上传到 PyPi

管道

代码中的每一个更改都可能会引入可能的错误。为了放弃这个,每次我们需要在正确的环境中测试自己的包时。

要做到这一点,除了 Python 本身之外还需要一些工具,比如 git、docker 和 make。

文档、许可证和其他通用文件

仅仅创建一个 Python 包并使其立即可供所有人使用是不够的。您还必须考虑如何记录它、向其他人简要解释它、许可它并解释如何集成到项目中。

这需要开发诸如 READMELICENSECODE_OF_CONDUCTCONTRIBUTING 等文件。
也许添加一个 CHANGELOG 来保存并让其他人跟踪对每个版本所做的更改。

几分钟内创建项目

要实现 Python 项目的所有部分,需要几个小时或几天的时间。
但有一个用于此目的的工具:psp。

按照安装说明进行操作后:

[test@ubuntu ~] sudo apt install -y python3 python3-pip git curl
[test@ubuntu ~] curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.1.0/psp.deb -o psp.deb
[test@ubuntu ~] sudo dpkg -i psp.deb
登录后复制
登录后复制

运行它:

[test@ubuntu ~] psp
Welcome to PSP (Python Scaffolding Projects): 0.1.0
> Name of Python project: app
> Do you want to create a virtual environment? Yes
> Do you want to start git repository? Yes
> Select git remote provider: Gitlab
> Username of Gitlab: test_user
> Do you want unit test files? Yes
> Install dependencies: flask
> Select documention generator: MKDocs
> Do you want to configure tox? Yes
> Select remote CI provider: CircleCI
> Do you want create common files? Yes
> Select license: Gnu Public License
> Do you want to install dependencies to publish on pypi? Yes
> Do you want to create a Dockerfile and Containerfile? Yes
Python project `app` created at app
登录后复制
登录后复制

现在,检查已创建的Python项目:

[test@ubuntu ~] ls -lah app
total 88K
drwxrwxr-x  9 test   test    440 Dec 20 14:48 .
drwxrwxrwt 29 root   root    680 Dec 20 14:49 ..
drwxrwxr-x  2 test   test     60 Dec 20 14:47 .circleci
drwxrwxr-x  7 test   test    200 Dec 20 14:47 .git
-rw-rw-r--  1 test   test    381 Dec 20 14:47 .gitignore
drwxrwxr-x  4 test   test     80 Dec 20 14:47 .gitlab
-rw-rw-r--  1 test   test    127 Dec 20 14:48 CHANGES.md
-rw-rw-r--  1 test   test   5.4K Dec 20 14:48 CODE_OF_CONDUCT.md
-rw-rw-r--  1 test   test   1.1K Dec 20 14:48 CONTRIBUTING.md
-rw-rw-r--  1 test   test    190 Dec 20 14:48 Containerfile
-rw-rw-r--  1 test   test    190 Dec 20 14:48 Dockerfile
-rw-rw-r--  1 test   test    35K Dec 20 14:48 LICENSE.md
-rw-rw-r--  1 test   test    697 Dec 20 14:48 Makefile
-rw-rw-r--  1 test   test    177 Dec 20 14:48 README.md
drwxrwxr-x  2 test   test     60 Dec 20 14:47 docs
-rw-rw-r--  1 test   test     19 Dec 20 14:47 mkdocs.yml
-rw-rw-r--  1 test   test    819 Dec 20 14:48 pyproject.toml
-rw-rw-r--  1 test   test     66 Dec 20 14:47 requirements.txt
drwxrwxr-x  2 test   test     80 Dec 20 14:47 tests
-rw-rw-r--  1 test   test    213 Dec 20 14:47 tox.ini
drwxrwxr-x  2 test   test     80 Dec 20 14:46 app
drwxrwxr-x  5 test   test    140 Dec 20 14:46 venv
登录后复制
登录后复制

开始开发包

开始开发 psp 命令为我们的项目创建的包。

[test@ubuntu ~] cd app/ && ls -lh app/
total 8.0K
-rw-rw-r-- 1 test test 162 Dec 20 14:46 __init__.py
-rw-rw-r-- 1 test test 204 Dec 20 14:46 __main__.py
[test@ubuntu ~] vim app/core.py
登录后复制
登录后复制
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Wow, this is my app!</p>"

登录后复制
登录后复制

现在,将我们的 hello_world 函数导入到 __main__.py 文件中:

pkg
├── __init__.py
├── module1.py
└── subpkg
    ├── __init__.py
    ├── __main__.py
    └── module2.py
登录后复制
登录后复制
[test@ubuntu ~] sudo apt install -y python3 python3-pip git curl
[test@ubuntu ~] curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.1.0/psp.deb -o psp.deb
[test@ubuntu ~] sudo dpkg -i psp.deb
登录后复制
登录后复制

运行我们的包

您已经编写了一个简单但有组织且功能强大的包,可以用于生产和分发。

测试我们的包。

[test@ubuntu ~] psp
Welcome to PSP (Python Scaffolding Projects): 0.1.0
> Name of Python project: app
> Do you want to create a virtual environment? Yes
> Do you want to start git repository? Yes
> Select git remote provider: Gitlab
> Username of Gitlab: test_user
> Do you want unit test files? Yes
> Install dependencies: flask
> Select documention generator: MKDocs
> Do you want to configure tox? Yes
> Select remote CI provider: CircleCI
> Do you want create common files? Yes
> Select license: Gnu Public License
> Do you want to install dependencies to publish on pypi? Yes
> Do you want to create a Dockerfile and Containerfile? Yes
Python project `app` created at app
登录后复制
登录后复制

结果是:

How to create own Python project in inutes

对包执行单元测试

现在还通过测试文件夹测试包上的 Python 代码:

[test@ubuntu ~] ls -lah app
total 88K
drwxrwxr-x  9 test   test    440 Dec 20 14:48 .
drwxrwxrwt 29 root   root    680 Dec 20 14:49 ..
drwxrwxr-x  2 test   test     60 Dec 20 14:47 .circleci
drwxrwxr-x  7 test   test    200 Dec 20 14:47 .git
-rw-rw-r--  1 test   test    381 Dec 20 14:47 .gitignore
drwxrwxr-x  4 test   test     80 Dec 20 14:47 .gitlab
-rw-rw-r--  1 test   test    127 Dec 20 14:48 CHANGES.md
-rw-rw-r--  1 test   test   5.4K Dec 20 14:48 CODE_OF_CONDUCT.md
-rw-rw-r--  1 test   test   1.1K Dec 20 14:48 CONTRIBUTING.md
-rw-rw-r--  1 test   test    190 Dec 20 14:48 Containerfile
-rw-rw-r--  1 test   test    190 Dec 20 14:48 Dockerfile
-rw-rw-r--  1 test   test    35K Dec 20 14:48 LICENSE.md
-rw-rw-r--  1 test   test    697 Dec 20 14:48 Makefile
-rw-rw-r--  1 test   test    177 Dec 20 14:48 README.md
drwxrwxr-x  2 test   test     60 Dec 20 14:47 docs
-rw-rw-r--  1 test   test     19 Dec 20 14:47 mkdocs.yml
-rw-rw-r--  1 test   test    819 Dec 20 14:48 pyproject.toml
-rw-rw-r--  1 test   test     66 Dec 20 14:47 requirements.txt
drwxrwxr-x  2 test   test     80 Dec 20 14:47 tests
-rw-rw-r--  1 test   test    213 Dec 20 14:47 tox.ini
drwxrwxr-x  2 test   test     80 Dec 20 14:46 app
drwxrwxr-x  5 test   test    140 Dec 20 14:46 venv
登录后复制
登录后复制

保存我们的作品

现在您可以节省网络应用程序的开发。

[test@ubuntu ~] cd app/ && ls -lh app/
total 8.0K
-rw-rw-r-- 1 test test 162 Dec 20 14:46 __init__.py
-rw-rw-r-- 1 test test 204 Dec 20 14:46 __main__.py
[test@ubuntu ~] vim app/core.py
登录后复制
登录后复制

测试环境

用docker模拟你的生产环境:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Wow, this is my app!</p>"

登录后复制
登录后复制

结果是一样的:
How to create own Python project in inutes

使用 make 构建管道

现在,下次开发后,您可以通过Makefile使用管道:

[test@ubuntu app] vim app/__main__.py
登录后复制

在 PyPi 上发布包

现在,如果您愿意,您就可以在 PyPi 上发布 Python 包了:

#! /usr/bin/env python3
# -*- encoding: utf-8 -*-
# vim: se ts=4 et syn=python:
# Generated by psp (https://github.com/MatteoGuadrini/psp)

from .__init__ import __version__
print(f'app {__version__}')

from .core import app
app.run(debug=True)

登录后复制

结论

不到五分钟,您就创建了一个 Python 项目,其中包本身的开发是您唯一需要担心的事情。

本文使用的工具:
psp:存储库 -- 文档
git: 存储库 -- 文档
docker: 存储库 -- 文档
make: 存储库 -- 文档
python: 存储库 -- 文档

以上是如何在 inutes 中创建自己的 Python 项目的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何解决Linux终端中查看Python版本时遇到的权限问题? 如何解决Linux终端中查看Python版本时遇到的权限问题? Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? 如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? 在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? Apr 01, 2025 pm 11:15 PM

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? 如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Apr 01, 2025 pm 10:51 PM

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

在Linux终端中使用python --version命令时如何解决权限问题? 在Linux终端中使用python --version命令时如何解决权限问题? Apr 02, 2025 am 06:36 AM

Linux终端中使用python...

如何绕过Investing.com的反爬虫机制获取新闻数据? 如何绕过Investing.com的反爬虫机制获取新闻数据? Apr 02, 2025 am 07:03 AM

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...

See all articles