My project directory looks like this:
project/
├─ new/
│ ├─ test.py
├─ docs.py
├─ main.py
Within my main.py, I import a function from docs.pylike this:
from docs import get_my_conn
and it works fine.
How can I import the same thing within new/test.py as well? I tried:
from ..docs import get_my_conn
but I get this error:
ImportError: attempted relative import with no known parent package
What you need to do is initialize the
newdirectory as a package. In order to do this, inside thenewdirectory make an empty file titled__init__.py. After you do this, go back to yourmain.pycode and import it like this instead:Your new tree should look like this:
P.S. If you are trying to import a function from docs.py into test.py, you probably should not do this. This is because it will result in an error known as a circular import. This will cause your script to no longer work. If you want to import a function from
docs.pyintotest.pythen put them in the same directory (or directory at the same level of the project hierarchy).