为什么customer python 的包裹无法导入?

Why customer python package can not be imported?

我创建了自己的 python 包,名为 jjnsegutils 并将其上传到 Pypi 网站。但是我pip install jjnsegutils安装成功后,还是无法导入。错误显示:ModuleNotFoundError: No module named 'jjnsegutils'.


详细流程如下。

包结构和详细信息

我的包的结构是:

jjnsequtils
├─ __init__.py
├─ myutil
    ├─ __init__.py
    ├─ myutil.py
├─ LICENSE
├─ README.md
├─ setup.py

两个 __init__.py 个文件是空的。

在我的 setup.py:

import setuptools

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

setuptools.setup(
    name="jjnsegutils", # Replace with your own username
    version="0.0.10",
    author="Jingnan",
    author_email="jiajingnan2222@gmail.com",
    description="A package of common utilities for Medical images segmentation and evaluation.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/Ordgod/jjnsegutils",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

如何生成包?

我使用命令python setup.py sdist bdist_wheel生成分发文件。 logging information:

running sdist
running egg_info
writing jjnsegutils.egg-info/PKG-INFO
writing dependency_links to jjnsegutils.egg-info/dependency_links.txt
writing top-level names to jjnsegutils.egg-info/top_level.txt
reading manifest file 'jjnsegutils.egg-info/SOURCES.txt'
writing manifest file 'jjnsegutils.egg-info/SOURCES.txt'
running check
creating jjnsegutils-0.0.10
creating jjnsegutils-0.0.10/jjnsegutils.egg-info
creating jjnsegutils-0.0.10/myutil
copying files to jjnsegutils-0.0.10...
copying README.md -> jjnsegutils-0.0.10
copying setup.py -> jjnsegutils-0.0.10
copying jjnsegutils.egg-info/PKG-INFO -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/SOURCES.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/dependency_links.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/top_level.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying myutil/__init__.py -> jjnsegutils-0.0.10/myutil
copying myutil/myutil.py -> jjnsegutils-0.0.10/myutil
Writing jjnsegutils-0.0.10/setup.cfg
Creating tar archive
removing 'jjnsegutils-0.0.10' (and everything under it)
running bdist_wheel
running build
running build_py
installing to build/bdist.linux-x86_64/wheel
running install
running install_lib
creating build/bdist.linux-x86_64/wheel
copying build/lib/myutil.py -> build/bdist.linux-x86_64/wheel
creating build/bdist.linux-x86_64/wheel/myutil
copying build/lib/myutil/__init__.py -> build/bdist.linux-x86_64/wheel/myutil
copying build/lib/myutil/myutil.py -> build/bdist.linux-x86_64/wheel/myutil
running install_egg_info
Copying jjnsegutils.egg-info to build/bdist.linux-x86_64/wheel/jjnsegutils-0.0.10-py3.7.egg-info
running install_scripts
adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
creating build/bdist.linux-x86_64/wheel/jjnsegutils-0.0.10.dist-info/WHEEL
creating 'dist/jjnsegutils-0.0.10-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it
adding 'myutil.py'
adding 'myutil/__init__.py'
adding 'myutil/myutil.py'
adding 'jjnsegutils-0.0.10.dist-info/LICENSE'
adding 'jjnsegutils-0.0.10.dist-info/METADATA'
adding 'jjnsegutils-0.0.10.dist-info/WHEEL'
adding 'jjnsegutils-0.0.10.dist-info/top_level.txt'
adding 'jjnsegutils-0.0.10.dist-info/RECORD'
removing build/bdist.linux-x86_64/wheel

如何分发包?

我使用命令twine upload dist/*dist中的文件上传到Pypi。

如何安装软件包?

我使用命令 pip install jjnsegutils 将其安装到我自己的名为 py37 的环境中,该环境由 conda 创建。它显示

Collecting jjnsegutils
  Downloading jjnsegutils-0.0.10-py3-none-any.whl (11 kB)
Installing collected packages: jjnsegutils
Successfully installed jjnsegutils-0.0.10

然后我在终端输入 $ python 进入 python 交互终端。并输入:

>>> import jjnsegutils

但是它显示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jjnsegutils'

我尝试了哪些操作来诊断问题?

我通过命令$ pip show jjnsegutils检查了包,它显示:

Name: jjnsegutils
Version: 0.0.10
Summary: A package of common utilities for Medical images segmentation and evaluation.
Home-page: https://github.com/Ordgod/jjnsegutils
Author: Jingnan
Author-email: jiajingnan2222@gmail.com
License: UNKNOWN
Location: /home/jjia/.conda/envs/py37/lib/python3.7/site-packages
Requires:
Required-by:

我进一步检查了 $ ls /home/jjia/.conda/envs/py37/lib/python3.7/site-packages,它显示:

... # other packages
jjnsegutils-0.0.10.dist-info                
... # other packages

我认为应该有另一个名为 jjnsegutilsjjnsegutils-0.0.10.dist-info 的目录。但是我没有看到。这是我无法导入自己的包的原因吗?

我确保虚拟环境在整个过程中始终是同一个名为 py37 的环境。我用的是CentOS,python3.7.

期待任何讨论和建议。非常感谢!

你应该import myutil.

packages is a list of all Python import packages that should be included in the Distribution Package. Instead of listing each package manually, we can use find_packages() to automatically discover all packages and subpackages. In this case, the list of packages will be example_pkg as that’s the only package present. https://packaging.python.org/tutorials/packaging-projects/