0

I have made a small programming language and seperated the Lexer, Parser, and Interpreter into different files. Now I would like those files to be in a sub directory Source. I have Shell.py that uses them. In short, this is the structure.

Language -{
   Source -{
       Main.py
       Lexer.py
       Parser.py
       Interpréter.py
   Shell.py

In shell .py, I want to import main.py, which in turn imports the Lexer, parser, and interpreter.

So:

#Shell.py
import Source.Main
Main.run(some code)

#Main.py
from Lexer import Lexer
.... Parser
.... Interpreter

When I run Main.py everything works, but when I run Shell.py it comes up with this:

File Source/Main.py, line 1 in <module>
    from Lexer import Lexer
ImportError: No module named ‘Lexer’

EDIT: There is an _init_.py in the Source directory. I am not trying to import multiple files from the sub directory, just one that procedes to import others.

  • 1
    you probably need an `__init__.py` and it also depends on your current working directory – Alexander Aug 09 '22 at 17:27
  • 1
    Does this answer your question? [How to load all modules in a folder?](https://stackoverflow.com/questions/1057431/how-to-load-all-modules-in-a-folder) – ptierno Aug 09 '22 at 18:01

1 Answers1

0

Importing a file does not automatically import all the packages you imported in the file. Basically, when you imported Main.py, you didn't import the packages that Main.py imports. You need to manually import all of the packages in that folder.

EDIT: Based on a discussion in the comments, you need to change import omega to from Source import omega assuming Source is what you called the folder where your other files is stored.

Craze XD
  • 110
  • 8