1

I have the following directory structure:

folder1
      \folder2
            file2.py
      \folder3
            file3.py

In file2.py there is a class that i would use in file3.py

The Problem is, that the from xx import yy command does not work, because the file2.py is not in the subdirectory of the file3.py

The following command from ..folder2.file2 import myclass_in_file2 does not work, too (Python 3.8).

Is there a easy and clear way to do this?

Tricks with "sys-path" and "exec" modifications may work, but I will not use them personally.

P.Schn
  • 11
  • 2
  • 1
    Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – jaypb Dec 17 '20 at 21:45
  • Turn the folders into packages or change the working directory to the target directory using os – TheLazyScripter Dec 17 '20 at 21:46
  • 1. Seems that it miss the __init__ file https://stackoverflow.com/questions/448271/what-is-init-py-for 2 Check this answer as well https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory 3 I suggest you to edit and remove or change the last bit of your question "Tricks with "sys-path" and "exec" modifications may work, but in my opinion, these are not good solutions." the my opinion part not work in StackOverflow as works with facts, just change it to 'Doesn't work for me' or 'I m looking for a different solution, just an advice – Federico Baù Dec 17 '20 at 21:50
  • This answer works: https://stackoverflow.com/a/61808596/14846759 & https://stackoverflow.com/a/61311572/14846759 But ists not really a nice solution, because the absolute project path is fixed to the path in the source files and debugging an jump to any subclass source file in the python IDE does not worked because the "import xx from yy" does not locate to the relative path of the source file. – P.Schn Dec 18 '20 at 08:09

0 Answers0