0

I have created a setup.py script for a gui application and a runner script myapp. The runner script contains:

#!/usr/bin/env python3

import myapp
myapp.gui_mode()

The application can be run from command line with python3 myapp.py Here is how myapp.py looks like:

#!/usr/bin/evn python3

def gui_mode():
    run_app()

def main():
    print("Starting UI")
    gui_mode()

if __name__ == '__main__':
    main()

After I install the app with sudo python3 setup.py install and attemp to run it from console with myapp, I receive the following message: AttributeError: module 'myapp' has no attribute 'gui_mode'

Ok, I start python3 interpreter and check:

import myapp
print(dir(myapp))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

I wonder, why is myapp getting imported without its own functions?

UPDATE

In [4]: print(myapp.__file__)
/home/tastyminerals/dev/NEFI2/nefi2/__init__.py

Here is the project structure:

APP2/
    setup.py
        myapp/myapp.py
        myapp/bin/run_myapp  <-- renamed runner file
        myapp/__init__.py
        myapp/data

Critical part of setup.py:

    packages=['myapp'],

    scripts=[
        'myapp/bin/run_myapp'
    ],

2 Answers 2

1

If you import the myapp.py you've shown here, it will have gui_mode and main attributes. I suspect that you're not loading the myapp.py module that you're trying to load. Check the value of myapp.__file__ to see which file you actually loaded.

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

1 Comment

It is loaded as __init__.py which is empty.
1

The runner script is named myapp and the module it depends on is also named myapp. Did you perchance name the package myapp as well? Something installed with setup.py has a base package name, so I'm guessing you probably named the package myapp too, so you'd actually need to import myapp.myapp (or rename myapp.py in the package to __init__.py so the package acts like a module of the same name).

It's a bad idea to overlap names like this in any event; if the runner script itself is named myapp.py, then it will import itself in preference to the installed myapp package; Python searches the local directory first, which is why it's a bad idea to name your scripts names that overlap Python built-in modules (e.g. never name a script math.py or you may screw up import math statements for other scripts in the same directory).

1 Comment

I've renamed the runner script, unfortunately to no effect.

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.