【问题标题】:installing from setup.py but ModuleNotFoundError从 setup.py 安装但 ModuleNotFoundError
【发布时间】:2021-10-31 07:26:03
【问题描述】:

我正在尝试制作一个 python 包。这是我的包结构。

my-package  
├── README.md  
├── LICENSE  
├── .gitignore  
├── setup.py  
└── my-package  
    ├── __init__.py
    └── other_files.py

在我的setup.py:

import io
import os
from setuptools import setup, find_packages, find_namespace_packages

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="new-package",    # This is the name of the package
    version="1.0.0",      # The initial release version
    author="Author Name", # Full name of the author
    author_email='author@gmail.com',
    url='https://github.com/Author/myPackage',
    description="Short Descriptio",
    long_description=long_description,      # Long description read from the the readme file
    long_description_content_type="text/markdown",
    packages=find_namespace_packages(),    # List of all python modules to be installed
    classifiers=[
        'Intended Audience :: Developers',
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        'Topic :: Software Development :: Internationalization',
    ],                                      # Information to filter the project on PyPi website
    python_requires='>=3.5',                # Minimum version requirement of the package
    install_requires=['requests','simplejson'],  # Install other dependencies if any
)

从根目录,我使用这个命令生成分发文件:

python setup.py sdist bdist_wheel

然后,我使用此命令从包根目录在虚拟环境中安装此包:

python -m pip install -e .

安装后,如果我运行pip list,它显示这个包(new_package)已安装。

但是当我尝试使用import new_package 导入它时,它给了我

ModuleNotFoundError: No module named 'new_package'

我在堆栈中搜索过类似的问题,但无法解决。

有什么帮助吗?

【问题讨论】:

    标签: python pip setuptools setup.py


    【解决方案1】:

    尝试将内部 my-package 文件夹重命名为 my_package,因为连字符不是要导入的 packagesmodules 的有效名称

    my-package  # root folder
    ├── README.md  
    ├── LICENSE  
    ├── .gitignore  
    ├── setup.py  
    └── my_package  # HERE, this package   
        ├── __init__.py
        └── other_files.py
    

    重建并重新安装发行版

    注意:根文件夹的名称 my-package 是否包含连字符无关紧要,因为它用于在 pypi 存储库上发布以及用于安装包的 pip 命令

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-07
      • 2018-01-23
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 2019-05-10
      • 2022-12-18
      相关资源
      最近更新 更多