I am trying to import a class named DataHandler into my test_data_handler file. I am doing this by stating from handles.data_handler import DataHandler. However, when I do this I get a error saying
No module named 'handles'
If anyone knows how to get round this issue it would be greatly appreciated!
You need add __init__.py file inside folder handles.
The __init__.py file lets the Python interpreter know that a directory contains code for a Python module.
import sys
sys.path.insert(1, '/path/to/application/app/folder')
from handles.data_handler import DataHandler
Importing files from different folder
Related
I have this project structure,
.\src
.\api
test.py
.\config
config.py
app.py
when i'm trying to import a function or class from test.py inside config.py, using this statement
from src.api.test import tes_func
I get this error
ModuleNotFoundError: No module named 'src.api'
if i use these 2 lines i can import using
from api.test import tes_func.
import sys
sys.path.append("../")
why it's not working when use from src.api.test import test_func
Is there a way to import python files without sys.path.append("../").
Thanks in advance.
You need to change the working directory.
Before python was only able to see other files inside the config folder.
If you run your program from a common parent directory python is able to see both folders and there files.
..\PycharmProjects\pythonProject> python -m src.config.config
My folder structure:
With this command src is now your root directory meaning in config.py you will need to import the function like this:
from src.api.test import tes_func
Hi, I've been stuck on this problem a while, and all other questions around this issue on this site haven't helped fix the issue.
In the main.py file, I'm trying to import the datastorer function which sits in the StoreData.py file.
I've created empty _init_.py files in the project root, DataStore
folder and also Calculations folder.
I'm importing using :
from project.DataStore.StoreData import datastorer
But always get the error
ModuleNotFoundError: No module named 'project.DataStore'.
I've tried adding
myDir = os.getcwd()
sys.path.append(myDir)
to the top of the file which wasn't successful.
I'm wondering if it's something to do with the name of the file main. Although I need to keep this as the file name. If anyone has any ideas this would be great thank you!
If you add your current folder to python path, then all you imports should not refer to project so try
from DataStore.StoreData import datastorer
At the top of main.py you can add
import sys
from os.path import dirname
sys.path.append(dirname(dirname(dirname(__file__))))
I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.
I was having some problem when trying to import method from another python class. I am following this guide. When I tried to run the attention.py, I am getting this error message:
File "D:\Desktop\tensorrec-master\examples\attention_example.py", line 8, in <module>
from test.datasets import get_movielens_100k
ModuleNotFoundError: No module named 'test.datasets'
I even tried to change the import statement to:
import test.datasets.py
test.datasets.get_movielens_100k(negative_value=0)
But I am still getting the same error message. Any ideas? Thanks!
Project structure needs to be known to your Python interpreter. And how to do it?
Set PYTHONPATH to base directory of your project
Add init.py file in each directory
Voila! You will be able to import from any directory under your base folder after that
From Import a file from a subdirectory? :
In short, you need to put a blank file named
__init__.py
in the "lib" directory.
I write some python files like this:
main.py
view/ __init__.py #empity file
MainWindow.py
ListEditor.py
And in each file I wrote those imports:
<main.py>
from view.MainWindow import MainWindow
...
-
<MainWindow.py>
from view.ListEditor import ListEditor
and ListEditor.py don't import any files.
Each MainWindow.py or ListEditor.py defines a class that named same as the file name.
when I run the program from main.py, it works. But when I run from MainWindow.py I got ImportError: No module named 'view'
If I write
from ListEditor import ListEditor
in MainWindow.py, python MainWindow.py will be OK. but python main.py will get error:
ImportError: No module named 'ListEditor'
So, is there a way to make both python main.py and python MainWindow.py get right at the same time?
I'm using python3.4
P.S.
I think I have figured out the problem here. The import command searches a module in sys.path. The sys.path is a group of predefined paths plus the running script path. When I run the code from MainWindow.py, the code import ListEditor just works, but when I run from main.py, the current path is set to the parent path. So I need import view.ListEditor.
Well, there are couple ways to deal with it. #Vincent Beltman's answer is one of it. Or just put these code in the __init__.py file:
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path)
Finally, I'm new to python. And I think the import command is quite strange. I thought it should search the files relative to the path of the source file that containing the command, not just relative to the starter file. A starter file may varying and cause troubles like this one.
Try this:
try:
from view.ListEditor import ListEditor # If this one fails
except:
try:
from ListEditor import ListEditor # It will try this one