I have the following directory structure:
C:/Automation/Windows/bin/powerconfig/powerconfig.py
C:/Automation/Windows/modules/Pylog.py
I am in the directory of:
C:/Automation/Windows/
When I try to run powerconfig.py file from a windows directory like
C:\Automation\Windows> pyhton \bin\powerconfig\powerconfig.py
I instantly get the error of no module named 'modules.Pylog'
Powerconfig.py contain import statement like
from modules.Pylog import Pylog
All directory contains __init__.py file to consider package
Even I tried to fix error by adding path C:/Automation/Windows/ to sys.path but still I am getting same error.
I don't know how to fix this error.
Try add the following code in Powerconfig.py
import sys
sys.path.append("C:/Automation/Windows/")
from modules.Pylog import Pylog
Related
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
I got an error. I created a module and it recognize it everywhere but did not work when i trined to import it in seedlingsWidget.py . I tried some solutions but nothing worked.
I tried to add init.py in every directory.
I tried to add the path of the directory to sys.path.append
from models.seedlings import Seedlings
how can I fix that?
I will be happy for some help, thank you
Try with
from models.seedlingsType import SeedlingsType
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 have a Python package with the following structure.
>python_package # package root directory
>app_one # subpackage directory
>__init__.py
>views.py
>app_two # another subpackage directory
>__init__.py
>views.py
Code for app_one/views.py:
def show():
print('do something')
Codes for app_two/views.py:
from app_one.views import show
show()
The problem is, whenever I try to run views.py of app_two from the terminal, I get an error
No module named 'app_one'
But when I open the package python_package in the PyCharm IDE, I'm getting no issue, everything works perfectly.
This error occurs because, the path to the file app_one is not in the current path, and you have to add it to the path using sys.path.append Try :
import sys
sys.path.append('./app_one')
from views import show
show()
I have created the same directory structure as you had and gave it a try and it is working.
I think what you were missing is that adding this line before you import in app_two/views.py:
sys.path.insert(0, os.path.abspath(__file__ + "../../../"))
Please have a look at the attached image which has detailed information
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.