0

This is probably a simple mistake on my part, but I just can't get this to work.

When I run a simple application which imports a custom package it works fine, but if I install the script + package with setuptools then it seems that the module is not found. I have dumbed this down to a really simple example with the following source structure:

.
├── hello
│   ├── __init__.py
│   └── message.py
├── hello.py
└── setup.py

hello.py contains just the following:

#!/usr/bin/env python3

from hello import MESSAGE

print(MESSAGE)

and message.py contains this:

MESSAGE = "Hello, World!"

and __init__.py contains:

from .message import MESSAGE

If I run this from my source tree it works fine:

$ ./hello.py 
Hello, World!

Now, I try to create an installer using setuptools like this:

from setuptools import setup

setup(
    name = "Hello World",
    version = "1.0.0",
    description = "Hello World",
    url = "http://www.example.com",
    author = "XX",
    author_email = "[email protected]",
    license = "XXX",
    scripts=["hello.py"],
    packages=["hello"]
)

I can install as a regular user like this:

$ python3 setup.py install --user

which puts everything under $HOME/.local/. When I try to run hello.py from the installed location I get the following error:

$ $HOME/.local/bin/hello.py
Traceback (most recent call last):
  File "./bin/hello.py", line 4, in <module>
    __import__('pkg_resources').run_script('Hello-World==1.0.0', 'hello.py')
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 658, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1445, in run_script
    exec(script_code, namespace, namespace)
  File "/home/XXX/.local/lib/python3.6/site-packages/Hello_World-1.0.0-py3.6.egg/EGG-INFO/scripts/hello.py", line 3, in <module>
    __requires__ = 'Hello-World==1.0.0'
  File "/home/XXX/.local/bin/hello.py", line 4, in <module>
    __import__('pkg_resources').run_script('Hello-World==1.0.0', 'hello.py')
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 658, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1445, in run_script
    exec(script_code, namespace, namespace)
  File "/home/XXX/.local/lib/python3.6/site-packages/Hello_World-1.0.0-py3.6.egg/EGG-INFO/scripts/hello.py", line 3, in <module>
    __requires__ = 'Hello-World==1.0.0'
ImportError: cannot import name 'MESSAGE'

So it seems the import can't find the module and MESSAGE is not imported.

What did I miss?

And, a second question, why does setup generate an egg rather than a wheel? I thought wheels are the preferred format now.

Some extra info for context:

OS = Linux Mint

Python version = 3.6.9

3
  • 1
    Do not use files with the same names as folders/ modules. Either rename hello.py or hello folder. Commented Apr 29, 2021 at 14:19
  • That was it! Now, any recommendations for naming packages like this, where there is a script with the same or name? Lets say I am writing an application xxx and I want to keep the script simple and put all the implementation in a package, what is the recommended package name in that case? Commented Apr 29, 2021 at 14:29
  • Personally, I always name the main file (the file that everything begins from) of my program as main_file.py. I also try to name folders based on what they contain, like a folder called constants that would contain multiple python files with constants in them. Commented Apr 29, 2021 at 16:51

1 Answer 1

1

OK, so as per @BoobyTrap 's comment, the solution was simple. renaming the hello directory to HelloLib and updating hello.py to import with the new name solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.