【问题标题】:pip package error after uploading to test.pypi.org上传到 test.pypi.org 后 pip 包错误
【发布时间】:2019-06-05 18:20:39
【问题描述】:

我已经创建了一个如下的 pip 包

我的 setup.py 文件

import setuptools

with open('README.md') as f:
    long_description = f.read()

setuptools.setup(
    name="calculator_py",
    version="0.0.1",
    scripts=['scripts/calculate.py'],
    author="xxxxxxx",
    author_email="xxxx@gmail.com",
    description="",
    long_description=long_description,
    long_description_content_type='text/markdown',
    url="",
    license='MIT',
    classifiers=[
    'Development Status :: 5 - Production/Stable',
    'Programming Language :: Python',
    'Programming Language :: Python :: 2',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6',
    'Programming Language :: Python :: 3.7',
    'Programming Language :: Python :: Implementation :: CPython',
    'Programming Language :: Python :: Implementation :: PyPy',
    'License :: OSI Approved :: MIT License',
    'Operating System :: OS Independent',

],
entry_points={
    'console_scripts': [
        'calculate=calculator.scripts.calculate:main',
    ],
}

)

我已经把这个包上传到https://test.pypi.org

在我使用安装包之后

pip install -i https://test.pypi.org/simple/ calculator_py

我已经使用

检查过包裹
pip list

当我尝试导入这个包时,它给了我以下错误

ModuleNotFoundError: 没有名为“calculator_py”的模块

calculate.py 文件

class calculate:

def __init__(self):
    pass

def add(self, arg1, arg2):
    return arg1 + arg2

def sub(self, arg1, arg2):
    return arg1 - arg2

def mul(self, arg1, arg2):
    return arg1 * arg2

def div(self, arg1, arg2):
    return arg1 / arg2

if __name__ == '__main__':
     calculate()

我该如何解决这个问题?

【问题讨论】:

  • 请修正缩进。
  • calculator_py 只是发行版的名称,您没有命名的模块或包,因此无需导入任何内容。运行pip show -f calculator_py 以查看安装了哪些文件。 calculate 可执行文件应该可用:使用 which calculate 检查它。顺便说一句,calculate=calculator.scripts.calculate:main 将不起作用,因为您的包中没有模块 calculator.scripts.calculate

标签: python python-3.x pip pypi


【解决方案1】:

您可能需要在您的setuptools.setup 中添加一个packages=['scripts'],例如。

setuptools.setup(
    ...
    packages=['scripts'],
    ...
)

否则 pip 可能会尝试从name="calculator_py" 猜测它,而您没有名为 calculator_py 的包。

【讨论】:

  • 首先,有scripts=['scripts/calculate.py'], 添加scriptspackages=['scripts'], 会很奇怪。其次,pip 不会从包名中猜测模块名。
猜你喜欢
  • 2019-10-25
  • 2023-03-23
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
相关资源
最近更新 更多