ImportError:安装我的pypi包

时间:2017-12-13 14:18:06

标签: python importerror pypi

在主目录中输入.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

2 个答案:

答案 0 :(得分:1)

__init__.py中添加getHello

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

答案 1 :(得分:0)

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

Python找到并加载模块的方式偶尔会令人惊讶。请参阅:https://docs.python.org/3/reference/import.html#searching

{ test: require.resolve('jquery-mockjax/dist/jquery.mockjax.js'), use: ['imports-loader?this=>window,exports=>""'] } 命令行选项与Python解释器一起使用,以确切了解正在发生的事情。

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