0

there is my project hierachy:

#__init__.py are empty files

folder
 |-- globalFunctions.py
 |-- __init__.py
 |-- monitor
     |-- file.py
     |-- __init__.py

I'm tring to import functions from globalFunctions.py, when I'm in the file.py file. I have tried to import it with

from .. import globalFunctions

but I'm getting

ImportError: attempted relative import with no known parent package

Do you know how to make it work?

Paul Lam
  • 176
  • 2
  • 12
xGoldeNx
  • 23
  • 7
  • Please read this : https://stackoverflow.com/questions/16981921/relative-imports-in-python-3 – Ram Jul 11 '21 at 12:30
  • From where do you import your main package "folder"? – 9769953 Jul 11 '21 at 12:33
  • Your code looks fine; I have just checked, and I can import your "folder" package (and modules like `folder.monitor` and `folder.monitor.file`) just fine, and access `folder.monitor.file.globalFunctions` without error. So there is some context that you've not yet mentioned (such as where you are doing the actual importing, see my previous comment). – 9769953 Jul 11 '21 at 12:36
  • Are you running `file.py` as a script, for example **python3 file3.py**? – Booboo Jul 11 '21 at 12:39
  • @Booboo yes, I'm running it on windows with "python -u file.py" command – xGoldeNx Jul 11 '21 at 12:45
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Booboo Jul 11 '21 at 12:45
  • Inside the file.py im using the functions from globalFunctions.py in this way: – xGoldeNx Jul 11 '21 at 12:46
  • proxyList = globalFunctions.getProxies() etc – xGoldeNx Jul 11 '21 at 12:46
  • You need to run it as a module: `python -m folder.monitor.file` – Booboo Jul 11 '21 at 12:47
  • I have tried running it with command python -m monitor.file, when I was in the folder file but I still recived ` attempted relative import with no known parent package` error – xGoldeNx Jul 11 '21 at 12:51
  • Okay, finally I got it. Thank you for your help and for the link to the ' Relative imports for the billionth time' question! – xGoldeNx Jul 11 '21 at 12:59
  • Don't run modules as file. The `-m` flag may help, but run things *outside* the package. Just don't "run" packages and modules until you understand what you are doing. – 9769953 Jul 11 '21 at 13:47

0 Answers0