user7988893
user7988893

Reputation:

ImportError: No module named after installing my pypi package

To vim .pypirc in home directory.

[distutils]
index-servers=pypi

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

The directory structure.

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

The getHello.py.

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

The 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='[email protected]',
    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'])
)

Now to make the pypi packages.

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

To list the file tree .

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

Upload the source file and whl file.

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

Now there are two files :getHello-1.0.0-py2.py3-none-any.whlin and getHello-1.0.0.tar.gz in my pypi account.

To install the package with pip.

sudo pip3 install getHello

Everything is ready for getHello.

python3
import getHello

An error occurs here:

ImportError: No module named 'getHello'

Why getHello.py can't be packed into getHello-1.0.0-py2.py3-none-any.whlin or getHello-1.0.0.tar.gz ? How to pack getHello.py into getHello-1.0.0-py2.py3-none-any.whlin or getHello-1.0.0.tar.gz ?

Upvotes: 1

Views: 3894

Answers (2)

user7988893
user7988893

Reputation:

To add a __init__.py in getHello:

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

Upvotes: 5

payne
payne

Reputation: 14177

Python is likely trying to import from the 'getHello' subdirectory in your tree (or, the getHello.py file), not the package you just installed.

The way Python finds and loads modules can be occasionally surprising. See: https://docs.python.org/3/reference/import.html#searching

Use the -v command line option with the Python interpreter to see exactly what's going on.

To avoid any confusion about what exactly is importing, start the Python interpreter from another directory (one that doesn't have a getHello subdirectory, or a file named getHello.py).

Upvotes: 0

Related Questions