Python 找不到新安装的模块

Python cannot find newly installed module

我使用以下 setup.py

创建了一个模块
# -*- coding: utf-8 -*-

# Learn more: https://github.com/kennethreitz/setup.py

from setuptools import setup, find_packages


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

with open('LICENSE') as f:
    license = f.read()

setup(
    name='mymod',
    version='1.0a1',
    description='test',
    long_description=readme,
    long_description_content_type="text/markdown",
    author='Ray Salemi',
    author_email='ray@raysalemi.com',
    url='https://rayboston@bitbucket.org/rayboston/mymod',
    license=license,
    packages=find_packages(exclude=('tests', 'docs', 'examples'))
)

但是当我尝试使用

安装它时
% python setup.py install

我看到它已安装在我的站点包中:

Processing mymod-1.0a1-py3.8.egg
Copying mymod-1.0a1-py3.8.egg to /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages
Adding mymod 1.0a1 to easy-install.pth file

Installed /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages/mymod-1.0a1-py3.8.egg
Processing dependencies for mymod==1.0a1
Finished processing dependencies for mymod==1.0a1
(testenv) (base) raysalemi@WriteNow mymod % cd ../testenv
(testenv) (base) raysalemi@WriteNow testenv % python
Python 3.8.3 (default, Jul  2 2020, 11:26:31)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymod'

我该如何调试它?我看不到错误。

我是 运行 Big Sur 11.0.1 和 Python 来自 Anaconda 的 3.8.3

pip 显示模块在那里

Package    Version
---------- -------
pip        20.3.1
mymod      1.0a1
setuptools 41.2.0

问题是包被错误命名:

(testenv) (base) raysalemi@WriteNow site-packages % ls
__pycache__         mymod-1.0a0-py3.8.egg
easy-install.pth        mymod-1.0a0.dist-info
easy_install.py         setuptools
pip             setuptools-41.2.0.dist-info
pip-20.3.1.dist-info        src
pkg_resources

mymod-1.0a0-py3.8.egg而不是mymod

要进行调试,您可以 运行 设置:

python setup.py sdist --formats=gztar

并解压缩生成的 .tar.gz 文件并检查是否所有源代码文件都在其中。 (或使用 --formats=zip 而不是 gztar 以获得更简单的文件来提取)

生成的包始终采用 package_name-package_version 格式,因此您收到的名称 并非 不正确。 (如果您想知道,您可以找到有效的 package_version 格式规则 here。)

您以后可以通过将此包添加到您要依赖它的项目的 requirements.txt 文件中来使用它。例如

my-package>=1.2.0,<2.0.0

在你的情况下,因为版本是预发布版本(mymod-1.0a0-py3.8.egg ==> 版本是 1.0a0-py3.8.egg 这意味着版本 1.0 预发布版本 alpha0-py3.8 ).

版本 1.0a0-py3.8.egg < 版本 1.0(预发布总是 < 相同编号的版本),所以你需要像 >0,<2.0.

这样的东西

就个人而言,我将源代码放在 src/ 下的 repo 中,然后 select 这些文件在 setup.py 中使用:

packages=find_namespace_packages(where="src")

我还推荐使用其他参数,例如确保环境有足够新的 setuptools 来识别 find_namespace_packages,从 requirements.txt 文件等中获取依赖项列表:

from setuptools import setup, find_namespace_packages


with open('requirements.txt') as f:
    required = f.read().splitlines()

setup(
    name='your_project_name',
    version='1.0.0',
    description='your project description',
    url='your repo url',
    author='your username',
    author_email='your email',
    license='your license type',
    package_dir={'': 'src'},
    setup_requires='setuptools>=45.2.0',
    packages=find_namespace_packages(where="src"),
    install_requires=required,
    data_files=['requirements.txt'],
    include_package_data=True
)

documentation 中查看选项的完整列表及其用途。

我发现了我的问题。

我的源目录被命名为 src 而不是 mymod。所以 site-packages 中有一个 src 目录,而不是 mymod 目录。这是一个惊喜,因为包在 setup.py.

中命名