我试图在函数级别的python包中映射函数和变量的用法/原因.有几个模块,其中函数/变量用于其他函数,我想创建一个类似于下面的字典:
{'function_name':{'uses': [...functions used in this function...],
'causes': [...functions that use this function...]},
...
}
Run Code Online (Sandbox Code Playgroud)
我所指的功能需要在包的模块中定义.
我该如何开始呢?我知道我可以遍历包__dict__并通过执行以下操作来测试包中定义的函数:
import package
import inspect
import types
for name, obj in vars(package).items():
if isinstance(obj, types.FunctionType):
module, *_ = inspect.getmodule(obj).__name__.split('.')
if module == package.__name__:
# Now that function is obtained need to find usages or functions used within it
Run Code Online (Sandbox Code Playgroud)
但之后我需要找到当前函数中使用的函数.如何才能做到这一点?有没有为这类工作开发的东西?我认为分析库可能需要做类似的事情.
TL;DR即使我已经使用install_requiresin指定了依赖项setup.py,但安装还是pip失败,因为找不到某些依赖项。
我开发了一个软件包,打算通过PyPi. 我创建了一个内置的分发轮并将其上传到,testPyPI以查看上传是否一切正常以及是否可以从用户角度安装该包。
但是,当我尝试pip在普通 python 2.7 环境中安装该包时,安装过程在安装依赖项时失败。
我的包依赖于这些包(我setup.py相应地将其添加到文件中):
...
install_requires=['numpy','gdal','h5py','beautifulsoup4','requests','tables','progress'],
...
Run Code Online (Sandbox Code Playgroud)
因此,当我运行 pip install 时,一切看起来都正常,直到收到此错误:
找不到满足进度要求的版本(来自#NAME#)(来自版本:)找不到进度的匹配发行版(来自#NAME#)
当我删除进度依赖项时(没有它我也可以生活),同样的事情也会发生pytables:
找不到满足要求的版本表(来自#NAME#)(来自版本:)找不到表的匹配分布(来自#NAME#)
如果我事先手动运行pip install tables,pip install progress一切都会按预期进行。
那么,我如何确保如果有人下载我的软件包,所有缺少的依赖项都会随之安装?
相关奖金问题:
MANIFEST.in如果模块不可用,我可以在包中包含一个wheel文件(也许通过)并将其安装为依赖项吗?如果是这样,怎么办?
我在python3.x中有一个Python包我一直在写,它与C库中的两个C函数交互.目前,我一直在使用ctypes,我直接链接到共享库*.so,然后使用python脚本与此进行交互.
https://docs.python.org/3/library/ctypes.html#module-ctypes
我现在很困惑如何分发这个python包,用户可以在github上或通过pip安装python包.不知何故,在安装时,需要下载,解压缩和编译C库.
问题是,C库包含几个与其他C库的依赖关系; 我目前静态链接这些库,现在整个C库通过cmake安装.
(1)是否可以通过cmake安装现在安装的C库?看来这应该完全在setup.py,对吗?
(2)在这种情况下,我对distutils/setuptools问题感到困惑.基于此链接,https://docs.python.org/3/extending/building.html#building
这样做的正确方法是通过distutils,而不是setuptools.但是,setuptools通常是在2018年创建/分发python包的推荐方法.上面的文档是否已过时?
我正在将配置从 迁移setup.py到setup.cfg,但我遇到了关键字问题cmdclass。我查看了 setuptools 文档,似乎没有记录或支持此关键字。所以我尝试了options.entry_points。但我不断收到无效命令错误。
这是我所拥有的:
setup.cfg
[options.entry_points]
console_scripts =
install = CustomInstall:run
Run Code Online (Sandbox Code Playgroud)
和
setup.py
from setuptools.command.install import install
from setuptools import setup
class CustomInstall(install):
def run(self):
print('overriden install command')
setup()
Run Code Online (Sandbox Code Playgroud)
结果只是一个正常的安装命令。但是,我想复制运行时得到的行为:
# setup.py
from setuptools.command.install import install
from setuptools import setup
class CustomInstall(install):
def run(self):
print('overriden install command')
setup(cmdclass= {"install": CustomInstall})
Run Code Online (Sandbox Code Playgroud)
它给出了一个覆盖的安装命令。
我正在构建自己的 Python 安装,然后使用 pip 安装一些包。我想将预构建的二进制轮用于Cryptography等软件包。
在 GNU/Linux 上就可以了:它抓住了 Manylinux1 的轮子,一切都工作得很好。
在 MacOS 上,它拒绝下载大多数版本的任何二进制轮。我已经为 pip 添加了很多-v选项,但它只说了:
$ mypython -s -u -m pip install -v --only-binary 'cryptography' 'cryptography==2.6.1'
...
Skipping link https://files.pythonhosted.org/packages/.../cryptography-2.6.1-cp27-cp27m-macosx_10_6_intel.whl#sha256=... (from https://pypi.org/simple/cryptography/) (requires-python:>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*); it is not compatible with this Python
...
Could not find a version that satisfies the requirement cryptography==2.6.1 (from versions: 1.0.1, 1.0.2, 1.1, 1.1.1, 1.1.2, 1.2, 1.2.1, 1.2.2, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.4, 1.5, 1.5.1, …Run Code Online (Sandbox Code Playgroud) 您可以使用以下命令获取 python 发行版的版本
import pkg_resources
pkg_resources.get_distribution("distro").version
Run Code Online (Sandbox Code Playgroud)
如果您知道发行版名称,那就太好了,但是我需要在运行时动态找出我的发行版名称。
# Common framework base app class, extended by each app
class App(object):
def get_app_version(self) -> str:
package_name = self.__class__.__module__.split('.')[0]
try:
return pkg_resources.get_distribution(package_name).version
except Exception:
return "development"
Run Code Online (Sandbox Code Playgroud)
这适用于应用程序包名称与分发名称相同的情况(例如requests)。然而,一旦它们不匹配(例如my-app包含 package my_app),这就会失败。
所以我需要的是发行版和它们的包之间的映射,我确定它一定存在于某个地方,因为 pip 似乎知道在调用卸载时要删除什么:
$ pip uninstall requests
Uninstalling requests-2.21.0:
Would remove:
/home/user/.virtualenvs/app/lib/python3.6/site-packages/requests-2.21.0.dist-info/*
/home/user/.virtualenvs/app/lib/python3.6/site-packages/requests/*
Run Code Online (Sandbox Code Playgroud)
如何以编程方式访问此映射?
我有以下问题:我有项目和一个小库以及 lib 的测试目录,类似这样:
项目\
+mylib\
++测试\
在 test__init__.py 中我有以下代码:
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, "test_data_files")))
Run Code Online (Sandbox Code Playgroud)
因此,只需更新 syspath 即可打开一些包含测试数据的文件。当我通过右键单击 Pycharm 中的目录来运行测试时,它会成功执行,但是当我运行时
python -m pytest lib/test
Run Code Online (Sandbox Code Playgroud)
它不会执行 test/ init .py 文件并因 FileNotFoundError 失败,因为它可以找到测试数据文件。
这种行为的原因是什么?我该如何解决?
谢谢。
假设我有以下目录结构:
\n\nsrc/\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 python/\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 generated/\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 a.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 lib/\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 b.py\nRun Code Online (Sandbox Code Playgroud)\n\n我的是什么setup.py为了创建目录布局如下的 dist,
src/\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 python/\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 a.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 lib/\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 b.py\nRun Code Online (Sandbox Code Playgroud)\n\n目标是简单地消除该generated文件夹。我尝试了无数的变化,package_dir但除了原始目录结构之外,无法生成任何内容。
我决定创建一个名为“NsmPY”的小模块。一旦我完成了模块背后的代码(可在 GitHub 上找到),我就开始尝试将这个新模块上传到 PyPi。然而,当我运行必要的命令时python3 setup.py sdist bdist_wheel,程序抛出一个错误:
running sdist
running egg_info
writing nsmpy.egg-info\PKG-INFO
writing dependency_links to nsmpy.egg-info\dependency_links.txt
writing top-level names to nsmpy.egg-info\top_level.txt
reading manifest file 'nsmpy.egg-info\SOURCES.txt'
writing manifest file 'nsmpy.egg-info\SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md
running check
creating nsmpy-1.0
creating nsmpy-1.0\nsmpy.egg-info
copying files to nsmpy-1.0...
copying setup.cfg -> nsmpy-1.0
copying setup.py -> nsmpy-1.0
copying nsmpy.egg-info\PKG-INFO -> nsmpy-1.0\nsmpy.egg-info
copying nsmpy.egg-info\SOURCES.txt -> nsmpy-1.0\nsmpy.egg-info
copying nsmpy.egg-info\dependency_links.txt -> nsmpy-1.0\nsmpy.egg-info
copying nsmpy.egg-info\top_level.txt -> nsmpy-1.0\nsmpy.egg-info …Run Code Online (Sandbox Code Playgroud) 使用Artifactory(https://cloud.google.com/artifact-registry)我打算添加对诗歌的依赖(https://python-poetry.org/docs/repositories/)。
我可以使用命令安装:(pip install --index-url https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/ <PACKAGE_NAME>使用 keyrings.google-artifactregistry-auth 进行身份验证)
我打算使用 POETRY 来管理我的依赖项。我不知道使用诗歌是否会增加这种依赖性。poetry add --source <PACKAGE_NAME> https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>我收到此错误:
poetry update
Updating dependencies
Resolving dependencies... (0.9s)
RepositoryError
401 Client Error: Unauthorized for url: https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/pytest/
Run Code Online (Sandbox Code Playgroud)
我的 pyproject.toml
...
[[tool.poetry.source]]
name = "SOME_LIB"
url = " https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/"
secondary = true
Run Code Online (Sandbox Code Playgroud)
这里有如何使用 PIP/VIRTUALENV 进行配置的详细信息:https://cloud.google.com/artifact-registry/docs/python/authentication但没有有关 Poetry 的详细信息。
您对此有什么建议吗?
python python-packaging python-poetry google-artifact-registry
python-packaging ×10
python ×8
pip ×4
python-3.x ×3
setup.py ×3
setuptools ×3
distutils ×2
ctypes ×1
function ×1
macos ×1
mapping ×1
pytest ×1
python-wheel ×1