3

I am pretty new to Python, but I encountered this problem when someone tried recommending me to use packages. My directory (not actual names but just for example's sake) are as follows:

Main_Folder
   - First_folder
       __init__.py
       first_file.py
   - Second_folder
       __init__.py
       second_file.py
   __init__.py
   third_file.py

I want to use some functions I created on first_file inside second_file so I wrote both (on different times, not same):

from .first_folder import first_file
from Main_Folder.first_folder import first_file

And I get errors like:

<from first import>
ModuleNotFoundError: No module named '__main__.first_file'; '__main__' is not a package
<from second import>
ModuleNotFoundError: No module named 'Main_Folder'

However, when I do an import for third_file to any of the files inside using, it WORKS:

from First_folder.first_file import some_function

So I was just wondering if i was doing something wrong. I know there are lots of questions like this existing and I already looked but i cannot get anything to work.. And I am new to Python too...

Update: I ran both codes using their full absolute path

ashiii
  • 31
  • 2
  • 1
    It probably depends on the directory you are in while executing the code – Khalil Al Hooti Nov 24 '18 at 23:55
  • What exactly were you running and from which directory? Which version of Python are you using? [PEP 328](https://www.python.org/dev/peps/pep-0328/#relative-imports-and-name) gives you a hint about the problem, it seems that `__main__` is set as the top level module (see also [this question](https://stackoverflow.com/q/41816973/3767239)). However I don't see how you get this with `..` relative import since you should get `ValueError: attempted relative import beyond top-level package` instead. – a_guest Nov 25 '18 at 00:26
  • If you are sitting in `Main_Folder` though and using `from .first_folder import first_file` (note the single `.`) in `third_file.py` and running `python third_file.py` then this perfectly explains the reported errors (as documented by [PEP 328](https://www.python.org/dev/peps/pep-0328/#relative-imports-and-name)). – a_guest Nov 25 '18 at 00:28
  • Thanks for the answers, I was running second_file.py. And also, you are right, it's only a single dot (.) and I get that error from using two dots. – ashiii Nov 25 '18 at 06:10
  • Also I ran my code on both Main_Folder and also at Second_Folder but still getting the same error.. – ashiii Nov 25 '18 at 06:12
  • @ashiii Please update your question with the exact import statements done in each of the files, as well as the directory from which you invoked whatever file. Right now the import statements and error message in your question don't coincide. The error message fits for single `.` relative import, not `..`. – a_guest Nov 25 '18 at 10:54

2 Answers2

0

One way to make this work is to add the parent path to the python path as follows:

import sys
sys.path
sys.path.append('..')

then you should be able to import normally

Pedro Borges
  • 1,240
  • 10
  • 20
0

I really like the description of relative package imports here: https://docs.python.org/3/reference/import.html#package-relative-imports (point 5.7). In your case, this should work:

from ..first_folder.first_file import first_function

or

from ..first_folder import first_file

Isi
  • 330
  • 3
  • 8