1

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
x89
  • 2,798
  • 5
  • 46
  • 110
  • Probably a duplicate of https://stackoverflow.com/questions/68960171/python-error-importerror-attempted-relative-import-with-no-known-parent-packa – mkrieger1 Apr 15 '22 at 22:57

2 Answers2

3

What you need to do is initialize the new directory as a package. In order to do this, inside the new directory make an empty file titled __init__.py. After you do this, go back to your main.py code and import it like this instead:

from new.test import (function)

Your new tree should look like this:

project/
├─ new/
│  ├─ test.py
|  ├─ __init__.py
├─ docs.py
├─ main.py

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.py into test.py then put them in the same directory (or directory at the same level of the project hierarchy).

Alan Shiah
  • 907
  • 1
  • 6
  • 19
  • I want to import a function from docs.py into test.py. Not the other way around – x89 Apr 16 '22 at 08:07
  • Yes, I am aware. Please refer to the PostScript. I explained that it would probably lead to a circular import error. – Alan Shiah Apr 16 '22 at 08:10
  • is there any other way of achieving it? without putting them in the same directory? Like using an __init__.py script as mentioned in the answer below – x89 Apr 16 '22 at 08:10
  • You can achieve it with __init__.py but in the long run, it's not a good idea. As your code structure starts to get more complicated, you'll end up importing things that depend on each other. You can refer to this [link](https://www.pythonpool.com/python-circular-import/) on circular import errors to learn more about them. – Alan Shiah Apr 16 '22 at 08:13
  • so are there any other alternatives??? also I didn't achieve it with init.py either. pls see my comment under the other answer – x89 Apr 16 '22 at 08:14
  • The link I posted goes over alternatives. – Alan Shiah Apr 16 '22 at 08:14
  • If you followed the steps correctly, you should be able to access it using `from docs.py import (function)`. If that doesn't work I would probably need to see your workspace. – Alan Shiah Apr 16 '22 at 08:21
0

The issue is in how you are running your code - you should add the init files as in

project/
├─ new/
│  ├─ test.py
   ├─ __init__.py
├─ docs.py
├─ main.py

then run your code as

python -m new.test # note no py

from the project folder.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • why is the init folder inside new? I want to import a function from docs into new/test.py. Not the other way around. Shouldnt init.py be on the same level as docs – x89 Apr 16 '22 at 12:47
  • nope, it didn't – x89 Apr 18 '22 at 12:29