【问题标题】:How to find the namespace of a package with pip如何使用 pip 查找包的命名空间
【发布时间】:2020-07-26 00:10:39
【问题描述】:

我正在尝试为 python 编写一个自定义包。它具有以下文件夹结构:

E:\GITHUB\FUNCTIONSYNTHESIZER
│   .gitignore
│   LICENSE
│   README.md
│   setup.py
│
├───docs
│       docs.md
│       Example-6-Points.png
│
└────src
    │   function_synth.py
    │   __init__.py
    │
    └───__pycache__

Pip 允许通过 setup.py 的文件夹路径进行安装。 setup.py 遵循Python.org 的指导方针,最重要的是在另一个名称不同的包中工作:

from distutils.core import setup

def README():
    return open("README.md").read()

setup(name='function_synthesizer',
    version = '0.0.2',
    description = 'Generates a polynomial interpolation from a set up points',
    author = 'David J. Kowalk',
    author_email = '...',
    url = 'https://github.com/davidkowalk/FunctionSynthesizer',

    license = "MIT",
    keywords = "math interpolation polynomial calculus",

    long_description = README(),
    long_description_content_type='text/markdown',

    classifiers=[
        Calssifiers here
    ],

    package_dir = {'':'src'},

    python_requires = ">=3.6",
    install_requires=['numpy'],

    project_urls={
        'Bug Reports': '...',
        'Funding': '...',
        'Source': '...',
    }

)

__init__.py 导入 function_synth 的不同功能。

from function_synth import solve
from function_synth import to_str
from function_synth import calculate

该软件包由 pip 以 setup.py 定义的名称“function_synthesizer”安装,但是当我运行 pip show function_synthesizer 时,我得到了

Name: function-synthesizer
Version: ...

这个名字违反了包名的语法,最重要的是没有在任何地方定义。我找不到该破折号存在的任何原因,并且由于某种原因它似乎替换了name 参数定义的下划线。但即使我删除了下划线,生成的破折号 python 也无法导入以下名称的包:

  • function_synthesizer
  • 函数合成器(破折号处的语法错误)
  • 函数合成器(去掉破折号后)
  • function_synth(文件名)

是否有从 pip 获取命名空间的方法以及如何在 setup.py 中定义该命名空间?

【问题讨论】:

  • 为了可读性,我排除了一些信息,我知道这些信息没有效果。
  • 忘记元数据中的 dist 名称,它在运行时不起作用。您的包结构错误,因为您根本没有打包任何源 - 通过发出 pip show -f function_synthesizer 检查它可能不会列出任何 python 文件。
  • 您的src 目录被声明为一个包(通过放置__init__.py)。是故意的吗?如果是,导入应该是from src.function_synth import ... 等,您应该通过packages=['src'] 在安装脚本中包含src 包。但是,这不是 src 目录在 src 布局中的用途。或者您只想安装function_synth.py,然后在安装脚本中使用py_modules 来包含它。
  • 文件 function_synth.py 在导入包时应该可以访问,我不希望有任何模块,因为只有一个模块将毫无意义。因此,我将 src 声明为一个包,并让 init.py 导入文件的功能。 @hoefling 是对的。 Pip 不列出任何 Python 文件。

标签: python python-3.x pip python-wheel


【解决方案1】:

通过添加包属性解决了问题:

packages = ["function_synthesizer"],
package_dir = {'function_synthesizer':'src'},

__init__.py 导入函数然后通过

from function_synthesizer.function_synth import function_name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多