[英]Python cannot find newly installed module
I've created a module using the following setup.py我使用以下 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'))
)
But when I try to install it using但是当我尝试使用安装它时
% python setup.py install
I see that it gets installed in my site packages:我看到它安装在我的站点包中:
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'
How do I debug this?我该如何调试? I can't see an error.
我看不到错误。
I'm running Big Sur 11.0.1 and Python 3.8.3 from Anaconda我正在从 Anaconda 运行 Big Sur 11.0.1 和 Python 3.8.3
Pip shows the module is there Pip 显示模块在那里
Package Version
---------- -------
pip 20.3.1
mymod 1.0a1
setuptools 41.2.0
The problem is that the package is being misnamed:问题是 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
It is mymod-1.0a0-py3.8.egg
instead of mymod
它是
mymod-1.0a0-py3.8.egg
而不是mymod
To debug you can run the setup:要调试,您可以运行设置:
python setup.py sdist --formats=gztar
and unzip the resulting .tar.gz
file and check if all your source code files are in it.并解压缩生成的
.tar.gz
文件并检查您的所有源代码文件是否都在其中。 (or use --formats=zip
instead of gztar
to get a simpler file to extract) (或使用
--formats=zip
而不是gztar
来获取更简单的文件来提取)
The resulting package is always of the form package_name-package_version
, so the name you received is not incorrect.生成的 package 始终采用
package_name-package_version
形式,因此您收到的名称不正确。 (In case you are wondering, you can find the valid package_version formatting rules here .) (如果您想知道,可以在此处找到有效的 package_version 格式规则。)
You can later use this package by adding it to the requirements.txt
file of the project you want to be dependent on it.您可以稍后通过将其添加到您想要依赖的项目的
requirements.txt
文件中来使用此 package。 Eg例如
my-package>=1.2.0,<2.0.0
In your case, since the version is a pre-release ( mymod-1.0a0-py3.8.egg
==> version is 1.0a0-py3.8.egg
which means version 1.0
pre-relase version alpha0-py3.8
).在您的情况下,由于该版本是预发布版本(
mymod-1.0a0-py3.8.egg
==> 版本是1.0a0-py3.8.egg
,这意味着版本1.0
预发布版本alpha0-py3.8
) .
The version 1.0a0-py3.8.egg
< than version 1.0
(pre-release always < release with same number), so you will need something like >0,<2.0
.版本
1.0a0-py3.8.egg
< 比版本1.0
(预发布总是 < 具有相同编号的版本),所以你需要类似>0,<2.0
的东西。
Personally, I put the source code in the repo under src/
and then select these files in setup.py using:就个人而言,我将源代码放在
src/
下的 repo 中,然后将 select 这些文件放在 setup.py 中,使用:
packages=find_namespace_packages(where="src")
There are other parameters I recommend using eg make sure environment has a new enough setuptools
to recognize find_namespace_packages
, take list of dependencies from requirements.txt
files etc.:我建议使用其他参数,例如确保环境有足够新的
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
)
See the full list of options and what they are for in the documentation .请参阅文档中的完整选项列表及其用途。
I found my problem.我发现了我的问题。
My source directory was named src
not mymod
.我的源目录被命名为
src
而不是mymod
。 So there was a src
directory in site-packages
instead of a mymod
directory.所以在
site-packages
中有一个src
目录而不是mymod
目录。 This is a surprise since the package is named in setup.py
.这是一个惊喜,因为 package 在
setup.py
中命名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.