1

I looked to several related questions:

Importing python file from other directory

and

how to import module from other directory in python?

but they do not really solve my problems.

So I have

|-1.py
|-my_app
  |-a.py
  |-b.py

From 1.py I did:

import sys
sys.path.insert (0, './my_app/')
from a import *

and I have the error: name a is not defined.

How could I call the class and functions I defined in a.py and b.py from 1.py?

Many thanks

mommomonthewind
  • 4,390
  • 11
  • 46
  • 74
  • Hello @mommomonthewind you can try to use [PYTHONPATH](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) or try this [link](https://docs.python.org/3/tutorial/modules.html#standard-modules)...Thankss – vaku Apr 25 '19 at 03:57
  • Try `from my_app.a import *` – bleand Apr 25 '19 at 04:01
  • It does not work :( @bleand – mommomonthewind Apr 25 '19 at 04:03
  • You should also do what @blhsing suggested to make it work – bleand Apr 25 '19 at 04:10
  • Why python has made simple import stuff from a relative path so complex? There are many Q on different portal and chain of complex solutions. In other C++, Java etc it is straight forward approach. – Krishna Jun 09 '20 at 04:29

1 Answers1

2

You need to have an __init__.py file (it can be empty) under the my_app directory for it to be an importable package.

blhsing
  • 91,368
  • 6
  • 71
  • 106