为什么客户的python包无法导入? [英] Why customer python package can not be imported?

查看:59
本文介绍了为什么客户的python包无法导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自己的名为 jjnsegutils 的python程序包,并将其上传到Pypi网站.但是,通过 pip install jjnsegutils 成功安装它之后,我仍然无法导入它.错误显示: ModuleNotFoundError:没有名为"jjnsegutils"的模块.

I created my own python package named jjnsegutils and upload it to Pypi website. But after I successfully install it by pip install jjnsegutils, I still can not import it. Error shows: ModuleNotFoundError: No module named 'jjnsegutils'.

有关整个过程的详细信息如下.

Details about the whole procedure are as follows.

我的包裹的结构是:

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 生成分发文件.记录信息:

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.

How do I distribute the package?

I use command twine upload dist/* to upload the files in dist to Pypi.

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

I use command pip install jjnsegutils to install it into my own environment named py37 created by conda. It shows

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交互式终端.并输入:

Then I type $ python in my terminal to enter the python interractive terminal. and type:

>>> import jjnsegutils

但是它显示:

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

我试图诊断出什么问题?

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

What actions I have tried to diagnosize the issue?

I checked the package by command $ pip show jjnsegutils, it shows:

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 进一步检查了它,它显示:

And I checked it futher by $ ls /home/jjia/.conda/envs/py37/lib/python3.7/site-packages, it shows:

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

我认为应该有一个名为 jjnsegutils 的笔记本目录以及 jjnsegutils-0.0.10.dist-info .但是我没有看到.这是我无法导入自己的软件包的原因吗?

I thought there should be anotehr directory named jjnsegutils along with jjnsegutils-0.0.10.dist-info. But I did not see it. Is this the reason that I can not import my own package?

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

I make sure that the virtual environment is always the same one named py37 during the whole procedure. I used CentOS, python3.7.

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

Looking forward to any discussion and advice. Thanks very much!

推荐答案

您应 import myutil .

packages是应该包含在分发程序包中的所有Python导入程序包的列表.无需手动列出每个程序包,我们可以使用find_packages()自动发现所有程序包和子程序包.在这种情况下,软件包列表将为example_pkg,因为这是唯一的软件包. https://packaging.python.org/tutorials/packaging-projects/

这篇关于为什么客户的python包无法导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆