1

I am trying to create a python package to distribute my code. I am not getting any error in creating a package, and installing created package.

However, after installation when I am trying to import the package I am getting error ModuleNotFoundError:

Following is the code

hello_world.py

class HelloWorld:
    def print_msg(self):
        print("Hello World")

setup.py

from setuptools import setup, find_packages
setup(
    name = "HelloWorld",
    version = "0.1",
    packages = find_packages(),
)

create package

▶ python setup.py bdist_wheel
running bdist_wheel
running build
installing to build/bdist.macosx-10.14-x86_64/wheel
running install
running install_egg_info
running egg_info
writing HelloWorld.egg-info/PKG-INFO
writing dependency_links to HelloWorld.egg-info/dependency_links.txt
writing top-level names to HelloWorld.egg-info/top_level.txt
reading manifest file 'HelloWorld.egg-info/SOURCES.txt'
writing manifest file 'HelloWorld.egg-info/SOURCES.txt'
Copying HelloWorld.egg-info to build/bdist.macosx-10.14-x86_64/wheel/HelloWorld-0.1-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.14-x86_64/wheel/HelloWorld-0.1.dist-info/WHEEL
creating 'dist/HelloWorld-0.1-py3-none-any.whl' and adding 'build/bdist.macosx-10.14-x86_64/wheel' to it
adding 'HelloWorld-0.1.dist-info/METADATA'
adding 'HelloWorld-0.1.dist-info/WHEEL'
adding 'HelloWorld-0.1.dist-info/top_level.txt'
adding 'HelloWorld-0.1.dist-info/RECORD'
removing build/bdist.macosx-10.14-x86_64/wheel

Installing package

~/PycharmProjects/test_dist ▶ pip install dist/HelloWorld-0.1-py3-none-any.whl
Processing ./dist/HelloWorld-0.1-py3-none-any.whl
Installing collected packages: HelloWorld
Successfully installed HelloWorld-0.1

~/PycharmProjects/test_dist ▶ pip freeze
HelloWorld==0.1

Error While importing module

>>> import HelloWorld
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'HelloWorld'
2
  • Is this just typo class HelloWord:? Commented Sep 26, 2019 at 1:47
  • @CodeRed yes, it's a typo. I fixed it. Commented Sep 26, 2019 at 1:52

1 Answer 1

2

Where is hello_world.py? Is it at the root folder adjacent to setup.py? Or in some subdirectory? I suspect the former. That means you don't have any packages so find_packages() returns an empty list so setuptools don;t package any code into the package.

Your hello_world.py isn't a packages (a directory with file __init__.py), it's a standalone module and such modules must be packed using py_modules. This is how you should write your setup.py:

from setuptools import setup

setup(
    name = "HelloWorld",
    version = "0.1",
    py_modules = ['hello_world'],
)
Sign up to request clarification or add additional context in comments.

2 Comments

both hello_world.py and setup.py files are directly into project folder. I made the changes you have suggested. however it's still giving the same error.
You cannot import HelloWorld if the module is named hello_world. Either import hello_world or from hello_world import HelloWorld.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.