ImportError: No module named after installing my pypi package

ImportError: No module named after installing my pypi package

到主目录中的 vim.pypirc。

[distutils]
index-servers=pypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = xxxx
password = xxxx

目录结构。

tree /tmp/getHello
/tmp/getHello
├── getHello
│   └── getHello.py
├── README.rst
└── setup.py

getHello.py.

cat /tmp/getHello/getHello/getHello.py
def say(something):
    print(something)

tmp/getHello/setup.py.

cat /tmp/getHello/setup.py
from setuptools import setup, find_packages
from codecs import open
from os import path
here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()
setup(
    name='getHello',
    version='1.0.0',
    description='getHello',
    long_description=long_description,
    url='https://upload.pypi.org/legacy/getHello',
    author='The Python Packaging Authority',
    author_email='pypa-dev@googlegroups.com',
    license='MIT',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
    ],
    keywords='sample setuptools development',
    packages=find_packages(exclude=['contrib', 'docs', 'tests'])
)

现在开始制作 pypi 包。

cd  /tmp/getHello
python3  setup.py sdist build
python3  setup.py bdist_wheel --universal

列出文件树。

tree
.
├── build
│   └── bdist.linux-x86_64
├── dist
│   ├── getHello-1.0.0-py2.py3-none-any.whl
│   └── getHello-1.0.0.tar.gz
├── getHello
│   └── getHello.py
├── getHello.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── README.rst
└── setup.py

上传源文件和whl文件。

python3  setup.py  sdist upload
python3  setup.py  bdist_wheel upload

现在我的 pypi 帐户中有两个文件:getHello-1.0.0-py2.py3-none-any.whlingetHello-1.0.0.tar.gz

使用pip安装包。

sudo pip3 install getHello

getHello 一切就绪。

python3
import getHello

这里出现错误:

ImportError: No module named 'getHello'

为什么getHello.py不能打包成getHello-1.0.0-py2.py3-none-any.whlingetHello-1.0.0.tar.gz? 如何将 getHello.py 打包成 getHello-1.0.0-py2.py3-none-any.whlingetHello-1.0.0.tar.gz ?

Python 可能会尝试从树中的 'getHello' 子目录(或 getHello.py 文件)导入,而不是您刚刚安装的包。

Python 查找和加载模块的方式有时会令人惊讶。参见:https://docs.python.org/3/reference/import.html#searching

-v 命令行选项与 Python 解释器一起使用,可以准确地查看发生了什么。

为了避免混淆到底导入了什么,从另一个目录(没有 getHello 子目录或名为 getHello.py 的文件)启动 Python 解释器。

getHello中添加一个__init__.py

cd  getHello/getHello
vim  __init__.py
from getHello.say import say