5

I have a directory structure like:

mycode
|
└-----dirA
|     └─---- fileA.py
└─----dirB
      └─---- fileB.py

How do I import fileB.object from fileA.py? I have __init__.py's in all the folders, including "mycode", but I continually get errors that I can't find fileB.py from fileA.py, and relative imports don't work either.

4
  • To debug add import sys print(sys.path) to top of the file you are running.. This will give some idea about what are the folders currently in system path Commented Jun 13, 2022 at 19:15
  • 2
    Have you considered turning your code into a package and installing it in your environment? Commented Jun 13, 2022 at 19:16
  • How do you start the program and from which directory? Relative imports don't work with the top level script because its not part of a package. Commented Jun 13, 2022 at 19:32
  • This question can't be answered without knowing how filaA.py is executed. Are you in the dirA directory and running as a script (e.g. python3 fileA.py)? Does some other script import mycode.dirA.fileA? In either case, the parent of mycode needs to be in sys.path. The usual way to do that is to make the package installable, and then install it. There are tricks (hacks) to get around that, but installing is the normal solution. Commented Jun 14, 2022 at 3:33

3 Answers 3

3

In python, there's a unique way of traversing the tree backwards:

  • One period . for same directory
  • Two periods .. for parent directory
  • Three periods for grand-parent...
  • You get the point

Try using:

from ..dirB.fileB import <symbol>
Sign up to request clarification or add additional context in comments.

5 Comments

This is the right answer, I didn't know about this part of the Python import system, +1. Anyway the Python's import system is a mess, unfortunately.
@FLAK-ZOSO - This is the right answer ... because you don't understand the import system? Ignore the upvotes. This won't work unless mycode is known as a package in python. The import system isn't a mess, its just that you have script executable paths and module paths (just like LIB paths in the operating system).
@tdelaney so you must sys.path.append the directory, right?
@tdelaney, if you want to import something that is not a direct descendant, this is a way to do it. There are additional ways, such as changing the PATH, which are not necessarily better or worse (it depends). You could argue that one should find a better way to structure their project tree, that's totally legitimate, but that's not the essence of the question IMHO.
@FLAK-ZOSO - You could insert or append. It just that the parent directory of mycode has to be in sys.path somewhere. Python will scan all of the directories in sys.path for the package name. But that raises a second issue, mypath must already have been imported somewhere. The top level script, which is not in the package, or some other module needs to import it.
0

if the python outputted the result that you can't find the folders, you must set the directory properly like

it should be C:\Users\Desktop\DirectoryName

instead of FolderDirectory:FolderName

i mean you should set the proper path of the directory, since a program is not really that smart that can locate it easily, but just a bunch of instructions that you wanted to do it properly

Comments

0

You may use sys.path.insert() method:

import sys
sys.path.insert(0, './subdirectory')
import myfile

and you can read more about it here

4 Comments

This is exactly what I wrote in an answer 2 minutes ago, but it got downvoted, so I guess the OP isn't searching for this kind of answer.
@FLAK-ZOSO there's a difference between sys.path.insert vs. sys.path.append. Since Python will load the module that comes first and ignores the rest, then the OP's module in question would need to be inserted to sys.path at the first position (index 0 in this case), unlike append which adds the given path to the end of the sys.path's list, which is what they might be not looking for or didn't work for them.
@BlockDiagram01, sorry to disappoint you, but if there are no collisions, append and insert will do exactly the same. I don't think collisions were the essence of the question here, but rather one script being able to access the second one.
@BlockDiagram01 - I don't understand insert verses append. It has to be in sys.path - preferably by installing the module. Its position in the path doesn't make any difference unless you have multiple like-named packages. And that's a problem in itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.