0

This question is related to this one. After going through the python's module documentation here, I'm unable to import a submodule into a script file included in a module that includes the respective submodule.

My setup is as follows:

python modules

where the source files contents are:

app.py

import sys

def app():
    print("Nice, this file has a __main__ entry point. It doesn't handle arguments though")


if __name__ == '__main__':
    #  python app.py 2>&1 | less +F
    app()

sys.exit(0)

launcher.py

import sys
from multiprocessing import Process
import test_module.test_submodule.app


p1 = Process(target=app, args=('no args yet',))
print("Launcher starting ...")
p1.start()

sys.exit(0)

(the init files are blank).

My configuration for pycharm is:

python config 2

Why can't the app method be called in the launcher script?

The following error is thrown when running the script:

Connected to pydev debugger (build 145.260)
Traceback (most recent call last):
  File "/home/Pycharm/pycharm-community-2016.1/helpers/pydev/pydevd.py", line 1530, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/Pycharm/pycharm-community-2016.1/helpers/pydev/pydevd.py", line 937, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/Pycharm/pycharm-community-2016.1/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "launcher.py", line 6, in <module>
    p1 = Process(target=test_module.test_submodule.app, args=('no args yet',))
AttributeError: 'module' object has no attribute 'test_submodule'
Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116

1 Answers1

0

Your launcher.py seems to be at the "test_module" level already. Did you try import test_submodule.app ?

EDIT

Try from test_submodule import app

Tino A.
  • 232
  • 3
  • 12
  • Yes, pycharm just exists the process. I've tried the following variations: import test_module.test_submodule.app. import test_submodule.app, no imports, specify the full path to app.py in the code. – Sebi Jun 27 '16 at 13:05
  • pycharm shows "Unresolved reference test_submodule". When I acutally run the code, it exits at the import. It really shouldn't matter how the import is being made as long as the scope of what is being imported is valid. – Sebi Jun 27 '16 at 13:19
  • Is `test_submodule` in the module search path? By default, it should be in the script directory (while testing, probably the present directory), or in the .../site-packages directory. – jcoppens Jun 27 '16 at 15:46
  • The search path is set to test_module and test_submodule is located in test_module – Sebi Jun 27 '16 at 16:05