0

Update: found answer


How can I import MyClass from classmodule?

directory structure:

/project
  /datagenerator
    __init__.py
    __main__.py
    classmodule.py
    setup.py

Ran the program: python datagenerator

Traceback (most recent call last):
  File "C:\Users\Matt\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\Matt\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "datagenerator\__main__.py", line 2, in <module>
    from .classmodule import MyClass
ImportError: attempted relative import with no known parent package

__main__.py importing MyClass

import sys
from .classmodule import MyClass
from .funcmodule import my_function


def main():
    print('in main')
    args = sys.argv[1:]
    print('count of args :: {}'.format(len(args)))
    for arg in args:
        print('passed argument :: {}'.format(arg))
    my_function('hello world')
    my_object = MyClass('Thomas')
    my_object.say_name()


if __name__ == '__main__':
    main()

I tried this:

from classmodule import MyClass

Even though __main__.py and classmodule are in the same directory there is unresolved reference

Unresolved reference 'classmodule'
el_pup_le
  • 11,711
  • 26
  • 85
  • 142
  • 1
    Possible duplicate of [PyCharm shows unresolved references error for valid code](https://stackoverflow.com/questions/11725519/pycharm-shows-unresolved-references-error-for-valid-code) – el_pup_le Jun 21 '18 at 02:55

2 Answers2

0

Invalid import syntax in your code. Instead, try this:

from classmodule import MyClass
from funcmodule import my_function
0

Your file structure should look like:

directory structure:

__init__.py file_a.py file_b.py

Oh, sorry for past answer, go ahead and name it __main__.py.

You need from . import foo or from . import * when the __init__.py file is present. Then you can import anything from the same folder.

mbowden
  • 687
  • 6
  • 7