繁体   English   中英

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'

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

我正在从 Anaconda 运行 Big Sur 11.0.1 和 Python 3.8.3

Pip 显示模块在那里

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

问题是 package 被错误命名:

(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 始终采用package_name-package_version形式,因此您收到的名称正确。 (如果您想知道,可以在此处找到有效的 package_version 格式规则。)

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

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
)

请参阅文档中的完整选项列表及其用途。

我发现了我的问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM