Skip to main content

自动导入和安装Python模块的工具

Project description

pythonrun

自动导入和安装Python模块的工具

PyPI version Python Versions License: MIT self testing

功能特点

  • 自动检测Python脚本中导入的模块
  • 自动安装缺少的依赖包
  • 智能处理本地模块和标准库
  • 支持递归检测导入
  • 支持命令行参数传递
  • 自动纠正部分包名(如将 PIL 纠正为 Pillow, torch 纠正为 torch torchvision torchaudio-这三个包需要一起安装,根据官网安装提示)
  • 自动读取requirements.txt

安装

从PyPI安装(推荐)

pip install pythonrun

从源码安装

git clone https://github.com/StevenLi-phoenix/pythonrun.git
cd pythonrun
pip install -e .

使用方法

pythonrun your_script.py [arg1 arg2 ...]

配置选项

你可以通过配置文件自定义pythonrun的行为,默认配置文件为 ~/.config/pythonrun/config.json

  • auto_install: 自动安装所有缺失的依赖,无需确认
  • auto_update_pip: 在安装依赖前自动更新pip
  • auto_read_requirements: 自动读取requirements.txt

示例

假设你有一个名为 example.py 的脚本,它使用了numpy和matplotlib:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(100)
plt.plot(data)
plt.title('Random Data')
plt.show()

如果你的系统没有安装numpy或matplotlib,使用pythonrun会自动安装它们:

pythonrun example.py

输出:

缺失的模块: ['numpy', 'matplotlib']
是否安装 numpy? (y/n): y
正在安装 numpy...
是否安装 matplotlib? (y/n): y
正在安装 matplotlib...
<运行你的python脚本>
[图表显示]

本地模块检测

pythonrun能够智能识别本地模块,避免尝试从PyPI安装它们:

# local_module.py
def hello():
    print("Hello from local module!")

# main.py
import local_module
local_module.hello()

运行 pythonrun main.py 不会尝试安装 local_module。

递归导入检测

pythonrun会递归检测导入的模块,确保所有依赖都被正确安装:

# module_a.py
import numpy

# main.py
import module_a

运行 pythonrun main.py 会检测到 numpy 的依赖并安装它。

开发者指南

安装开发环境

git clone https://github.com/StevenLi-phoenix/pythonrun.git
cd pythonrun
pip install -e ".[dev]"

运行测试

pytest tests/test_entry.py

最近更新

  • 修复了递归导入时可能导致的无限循环问题
  • 增强了错误处理和异常捕获
  • 改进了文件操作的安全性
  • 添加了包名验证,防止恶意注入
  • 添加了网络请求和安装操作的超时控制
  • 优化了标准库模块的更新逻辑

贡献

欢迎通过Issue或Pull Request提供反馈和建议。

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythonrun-1.1.6.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythonrun-1.1.6-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file pythonrun-1.1.6.tar.gz.

File metadata

  • Download URL: pythonrun-1.1.6.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pythonrun-1.1.6.tar.gz
Algorithm Hash digest
SHA256 04108d5b885a6bf33a7d3b9002f183d22681fa218a672e240329f4fac1a594f1
MD5 56b8f5459f7c97307f877bd020b1c28c
BLAKE2b-256 76d8e0faa5459ed6af0bac8996beaadf8a7d11901f9f28104d8836a166e92da5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonrun-1.1.6.tar.gz:

Publisher: release.yml on StevenLi-phoenix/pythonrun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pythonrun-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: pythonrun-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pythonrun-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d01541c823bee0bd5424e552e629856acb0d3d01bfe2347dca76e413594b8339
MD5 fc5341654a99ccea21d41ab611da82de
BLAKE2b-256 1226e569c9631610bb291fe83229f7e6d9b74e806dc8351aa4a72cdd1e32a4fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonrun-1.1.6-py3-none-any.whl:

Publisher: release.yml on StevenLi-phoenix/pythonrun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page