Reputation: 141
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
Upvotes: 0
Views: 148
Reputation: 141
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.
Upvotes: 1