ModuleNotFoundError: No module named python - python

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

Related

How to resolve Python Module Not Found Error?

I am facing a problem in importing modules in python. I looked for a solution and found this. but this did not worked either.
My Directory is as follows
->MyScrapper --->MyScrapper ----->db_connection.py --->Video_Scrapper -----> video_scrapper.py --->Blogs_Scrapper -----> blogs_scrapper.py
I want to import db_connection.py in video_scrapper.py as well as blogs_scrapper.py. I have tried to import it as from MyScrapper.db_connection import DBConnection. It throws a ModuleNotFoundError: No Module named 'MyScrapper'. I have also tried using from db_connection import DBConnection and import DBConnection but none of them worked.
Please Help!!
When importing a module in python, it will search the module in this order:
build-in module
script in the current directory
PATHPYTHON
installation-dependent default
So, you need to add the MyScrapper module to the search path. If you use a virtual environment, you can add your project root directory into the PYTHONPATH, by modify the PYTHONPATH in the Scripts\activate.bat file, like this:
set PYTHONPATH=%VIRTUAL_ENV%\src;%PYTHONPATH%
I use the virtual environment, and my project struct is:
|--Include
|--Lib
|--Scripts
|--src
My source files are in the src directory.

main files and module import errors

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__))))

Importing from sibling directory

I have searched this question over stackoverflow but can't find an answer that fixes this. I am trying to learn how to do proper imports with python.
I am using Python 3.8.2 and I have the following simple directory setup.
main_folder\
folder1\
myclass.py
folder2\
testclass2.py
testclass1.py
Both testclass1.py and testclass2.py have this inside:
from folder1.myclass import Myclass
This works fine for testclass1.py, but when I run in testclass2.py it gives me an error.
ModuleNotFoundError: No module named 'folder1'
Even though I had read Python no longer requires this, I inserted an __init__.py file into folder1. This generated the same error. I then tried following directions for the init file in this article but there was no improvement. I also tried using relative paths versus absolute paths for import, but no success.
Help is much appreciated.
From testclass2.py the import should be like this:
from ..folder1.myclass import Myclass

I am getting an import error. How to fix this?

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

Python package import error

I'm trying to package my modules, but I can't seem to get it working.
My directory tree is something like the following:
snappy/
__init__.py
main/
__init__.py
main.py
config.py
...
...
and the code I'm using is
from snappy.main.config import *
I'm getting the error:
ImportError: No module named snappy.main.config
Any ideas what's going wrong? This is using Python 2.5 on Ubuntu 8.10.
Thanks in advance for your help.
It depends on where your script using the import resides and your system PYTHONPATH. Basically, to have that import working you should run your script (the one having the import) in the parent directory of snappy or your script should change sys.path to include it.
./alex
Is the parent directory of snappy in sys.path? If it's not, that's the only thing I can think of that would be causing your error.

Categories

Resources