English is not my mother tongue, so there might be some grammatical errors in my question.
Sorry about that.I git clone a project from github to my VScode. When I wanted to run demo code, a "ModuleNotFoundError" occured. I was confused about this error. Because I checked module and it did exit, I also haven't install same name module before. Here is the project-tree of the project.(Only parts including "SLCT" are given)
source code
└─ logparser_root
├─ benchmark
│ ├─ SLCT_benchmark.py
├─ demo
│ ├─ SLCT_demo.py
├─ logparser
│ ├─ SLCT
│ │ ├─ cslct.c
│ │ ├─ cslct.h
│ │ ├─ README.md
│ │ ├─ SLCT.py
│ │ ├─ __init__.py
│ │ └─ __pycache__
│ │ └─ __init__.cpython-39.pyc
"SLCT_demo.py" import SLCT from logparser.
import sys
import os
# sys.path.append('../')
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# sys.path.append('../logparser//SLCT//SLCT.py')
# print(sys.path)
from logparser import SLCT
When I ran "SLCT_demo.py", this error occured.
Traceback (most recent call last):
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\demo\SLCT_demo.py", line 9, in <module>
from logparser import SLCT
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\logparser\SLCT\__init__.py", line 1, in <module>
from SLCT import *
ModuleNotFoundError: No module named 'SLCT'
Here is "init.py" of "SLCT".from SLCT import *
Thanks for spending time on my question. Have a nice day!
In order to run from SLCT import * inside file x.py, you need to have the following directory structure:
File x.py
Folder SLCT
- File __init__.py
In your case, you are trying to run from SLCT import * inside file __init__.py, which means that you need to have the following directory structure:
File __init__.py
Folder SLCT
- File __init__.py
And in your case, since this whole thing is already inside a folder named SLCT, you're probably caught in your own confusion.
In short, it sounds like you want to simply move the entire contents of file SLCT.py into file __init__.py (although it's hard to say for sure, because you haven't made the rest of your code visible).
Related
I have the following project structure:
my-app/
├─ README.md
├─ LICENSE
├─ tests/
│ ├─ my_test.py
├─ src/
│ ├─ utils/
│ │ ├─ __init__.py
│ │ ├─ utils.py
│ ├─ __init__.py
│ ├─ app.py
And my app.py (located in src) uses some functions of utils.py. When I run python src/app.py it works fine, but, when I run python -m unittest tests.my_tests for the tests code (which instantiates the main class at app.py) I get the following error:
from utils.utils import *
ModuleNotFoundError: No module named 'utils'
i want to access config/cache.py from test.py And ,I need internal access like from config/env.py to database/modules/game.py but i get this error always:
ModuleNotFoundError: No module named 'modules'
my project tree:
project
├─ modules
│ ├─ cache
│ │ ├─ playtime.py
│ │ └─ __init__.py
│ ├─ config
│ │ ├─ cache.py
│ │ ├─ database.py
│ │ ├─ env.py
│ │ ├─ yaml.py
│ │ └─ __init__.py
│ ├─ database
│ │ └─ models
│ │ ├─ game.py
│ │ ├─ member.py
│ │ ├─ room.py
│ │ ├─ wiki.py
│ │ └─ __init__.py
│ ├─ room
│ ├─ utils
│ │ ├─ functions.py
│ │ ├─ logger.py
│ │ └─ __init__.py
│ └─ __init__.py
├─ run.py
└─ test
└─ test.py
and inits file are like that:
from .config import *
from .utils import *
from .database import *
from .cache import *
Python automatically insert the folder in which the main script resides to sys.path, which is a list containing folders where to find packages and modules.
So, if test.py were in folder project, that folder would be in sys.path and then you could import the modules package and any of its subpackages or modules.
import module # main package module
from module import config # subpackage config
from module.config import env # module env
(Just in case, a module is a file ending in .py, and a package is a folder with a file called __init__.py inside and optionally subpackages and/or modules.)
So one solution wood be to move the file test.py to projects
Another alternative is to add the folder projects (as a str) manually to sys.path which is a simple python list before importing.
import sys
sys.append('.../projects')
from modules.config import env
I would like to outsource some general functions useful for multiple notebooks in a module (also for testing purposes). The current directory structure looks like the following
jupyter/
├─ notebooks/
│ ├─ 01 Notebook 1.ipynb
│ ├─ ...
├─ src/
│ ├─ module_a/
│ │ ├─ __init__.py
│ │ ├─ func_a.py
│ ├─ module_b/...
├─ tests/...
├─ data/...
├─ .../
In func_a.py, there is a simple function def print_a(): print('a')
However, when I would like to import and use module_a in 01 Notebook 1.ipynb by using (what I think makes sense)
from .. src.module_a import print_a
I got an ImportError: attempted relative import with no known parent package. What am I doing wrong? I am using Python 3.9.
I would try to append the src directory to the system path like so:
import sys
sys.path.append("/path/to/your/src")
from src.module_a import a
please note that you can use the relative path from your notebook root and not absolute path like in the example above, so the following:
sys.path.append("src")
should work too
I am currently constructing a custom module with an architecture like such:
module/
│
├── util/
│ ├── metrics.py
│ └── util.py
│
└── training/
├── pretraining.py
└── training_step.py
In my pretraining.py script I need to use a function located in util.py. I am not sure how to import it in pretraining.py
So far I tried the classic:
from util.metrics import accuracy
I get the following error:
Traceback (most recent call last):
File "pretraining.py", line 5, in <module>
from util.metrics import accuracy
ModuleNotFoundError: No module named 'util'
How can I import the function in pretraining.py?
As PCM indicated you have to create an empty __init__.py file inside each folder:
module/
├── __init__.py
│
├── util/
│ ├──__init__.py
│ ├── metrics.py
│ └── util.py
│
└── training/
├──__init__.py
├── pretraining.py
└── training_step.py
So if in your pretraining.py script you need to use a function located in util/metrics.py:
from module.util.metrics import accuracy
Some references:
https://github.com/Pierian-Data/Complete-Python-3-Bootcamp/blob/master/06-Modules%20and%20Packages/Useful_Info_Notebook.ipynb
https://docs.python.org/3/tutorial/modules.html#packages
https://python4astronomers.github.io/installation/packages.html
Clearly, you are trying to import from another folder. For that, you need to make it a package
You need to save an empty __init__.py file in the module folder, and subfolders.
__init__.py will make it a package, so you can import it using
from util.metrics import accuracy
I would like to load in a function/module with my working directory as project main directory but the function file is store below the a subdirectory level so the normal
from function_file import function_name
does not work.
This is what the project directory set up looks like:
└───project_main_directory
│ .gitattributes
│ .gitignore
│ README.txt
│
├───code
│ ├───exploration
│ └───scripts
│ │ script1.py
│ │ script2.py
│ │ script3.py
│ │
│ └───functions
│ │ function1.py
│ │ function2.py
│ └───__init__.py
│
├───data
│ └───example_data
│ data.csv
└───documents
So I tried to import functions via
import code.scripts.function.function1 from function1
and that doesn't work. I know it's because the other subdirectories aren't modules, but I want to ask if there is away around that?
-- EDIT
I'm working from .py file in code/scripts/script1.py but working directory is project_main_directory/
Add an empty file __init__.py to every subdirectories to make them as modules.
.
├── code
│ ├── __init__.py
│ └── scripts
│ ├── __init__.py
│ └── script1.py
└── main.py
Then if you have a function called hello in code/scripts/script1.py you can import that function by:
from code.scripts.script1 import hello
hello("yo")
If your current directory is project_main_directory, you can use:
from code.scripts.functions.function1 import function1
Directory of your script doesn't matter. Only your current directory matters (refer top of the IDE)
To import function/module from another python file, you have to do something like below -
from code.scripts.functions.function1 import function1
Above we are loading function1 from function1.py file which is stored in functions directory which is stored in scripts directory and finally in code directory.
EDIT - so you are saying, you want to load a function from function1.py in script1.py? In that case from .functions.function1 import function should work.