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.
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.
Related
I am trying to import from a specific python file (which I wrote) class that in that file and it seems that python just does not recognize my file.
add image to represent the situation more deeply:
that the file I want to import the Ball class from:
the main file and the error:
mylib
the files
To import from the same directory, put a dot at the front of the module name like:
from .shapes import Ball
But if you are running main_Pygame.py as the script then this will not work. You should create a subdirectory for shapes.py, say mylib and then make an empty file called __init__.py there and mve shapes.py to that directory. Then import like:
from mylib.shapes import Ball
The __init__.py file tells python that the mylib directory (or whatever else you wish to call the directory) is part of the directory tree it should look for modules under.
I am working on some python project (2.7) and I have issue with imports. When I run main.py it start scripts from tests folder etc. and save output to logs and everything works fine.
/root------------
-logs
-staticCfg
-config.py
-tests
-systemTest
-scrypt1.py
-scrypt2.py
-userTest
-uScrypt1.py
main.py
My static variables (email, name etc.) are located in config.py. I need to import config.py in scrypt1.py or scrypt2.py. I tryed adding __init__.py to tests, systemTest and staticCfg folder but I always get an error.
In my scrypt1.py:
import staticCfg as cfg
...
or
from staticCfg import *
...
I get the error:
ImportError: No module named staticCfg
The import mechanism of Python can be a bit tricky.
You can refer to the documentation for more information: Python Import Mechanism
When you use absolute imports (your import does not start with a .) as you do, the import path will start from your main script (the one you launch). In your case, it's scrypt1.py. So starting from this location, python can't find the package staticCfg.
For me, the simplest solution is to create a main script in your root directory and call scrypt1.py from there (imported using from tests.systemTet import scrypt1.py). In this case, the base package will be your root folder and you will have access to the package staticCfg from all your script files as you wanted to do.
you may add root folder to PYTHONPATH.
I have a problem with python directories.
I have structure like:
python
modules
algorithms
cyphers
morse.py
tests
algorithms
cyphers
test_morse.py
When I try to import module in test I get a module not found error.
I am trying to import like:
parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(parent_dir_name + "/../../../")
from modules.algorithms.cyphers.morse import *
I have also init files in all directories.
I am trying to run tests like:
> python -m unittest discover ./tests
It is customary to use relative imports
from ....modules.algorithms.ciphers.morse import *
This ensures that you import the right Morse file, otherwise you risk importing some module named Morse that you've installed.
Here's two examples to illustrate relative imports, say you have a file named simulation.py that has the line
from .animals import Herbivore
In it. This will import the Herbivore class from a file in the same directory as the simulation.py file
Now imagine that you have a folder named tests, in this folder you have a file named test_animals.py, which has the line
from ..animals import Herbivore
This will import the Herbivore class from a file named animals.py located in the folder above the tests folder.
Finally, note that (at least for Python 3.6) can't run a file that uses relative imports, only import it.
If you mark the directories as modules with __init__.py, you should just be able to import modules via
from modules.algorithms.cyphers import morse
I am trying to import a module from a python file that is in a sibling folder. I read several similar questions here and tried to apply solutions listed there, but I wasn't able to solve the problem.
The structure is as follows:
parentfolder/gfolder/codefolder/fileA.py
parentfolder/gfolder/utilfolder/util.py
gfolder, codefolder and utilfolder all have an __init__.py.
I'm trying to do in fileA.py:
import gfolder.utilfolder.util as util
I also tried adding before the import statement:
sys.path.append(".../parentfolder/")
And that didn't work either:
import gfolder.utilfolder.util as util
ModuleNotFoundError: No module named 'gfolder'
The solution in a similar question says to include __init.py__ in the directories, which I already have.
EDIT:
Now both sys.append and sys.insert work and the problem was that I included a slash at the end of the path. When I took it out, everything worked.
First of all, let me describe you the differences between a Python module & a Python package so that both of us are on the same page. ✌
A module is a single .py file (or files) that are imported under one import and used. ✔
import aModuleName
# Here 'aModuleName' is just a regular .py file.
Whereas, a package is a collection of modules in directories that give a package hierarchy. A package contains a distinct __init__.py file. ✔
from aPackageName import aModuleName
# Here 'aPackageName` is a folder with a `__init__.py` file
# and 'aModuleName', which is just a regular .py file.
Therefore, when we have a project directory named proj-dir of the following structure ⤵
proj-dir
--|--__init__.py
--package1
--|--__init__.py
--|--module1.py
--package2
--|--__init__.py
--|--module2.py
🔎 Notice that I've also added an empty __init__.py into the proj-dir itself which makes it a package too.
👍 Now, if you want to import any python object from module2 of package2 into module1 of package1, then the import statement in the file module1.py would be
from proj-dir.package2.module2 import object2
# if you were to import the entire module2 then,
from proj-dir.package2 import module2
I hope this simple explanation clarifies your doubts on Python imports' mechanism. 😊
As Andrew Cox answerd int the following thread Import a module from a relative path
You can add the subdirectory to your Python path so that it imports as a normal script
import sys
sys.path.insert(0, <path to gfolder>)
import gfolder
you can also add the directory to the PATH var of the Linux system (I use it while I'm working on a project, at the end i modified the PATH to it's origin value)
if you maintain the following structre than it is working out side the box
parentfolder/gfolder/codefolder/fileA.py
parentfolder/gfolder/utilfolder/util.py
parentfolder/gfolder/main.py
run main.py
I am trying to import my own files in Python3. My directory looks like this:
/path/folder/__init__.py
/path/folder/custom_module2.py
/path/folder/custom_module.py
/path/launcher.py
init:
import custom_module
custom_module:
import custom_module2
def custom_function:
custom_module2.custom_function()
print('world')
custom_module2:
def custom_function2:
print('hello')
launcher:
import folder
custom_function()
it says that there is no module named custom_module
Actually, if you just put an empty file called __init__.py file, python will treat the directory as a package and you can import it with the dot notation.
__init__.py
custom_module2.py
custom_module.py
then from launcher.py
from folder.custom_module import custom_function
custom_function()
The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.
Source: https://docs.python.org/3/tutorial/modules.html#Packages