Python - No module named - python

I have the following code with few modules:
import Persistence.Image as img
import sys
def main():
print(sys.path)
original_image = img.Image.open_image()
if __name__ == "__main__":
main()
(I've created my own Image module)
And so I'm getting the following error claiming that the Persistence module does not exist:
Traceback (most recent call last):
File "/home/ulises/PycharmProjects/IntelligentPuzzle/Puzzle.py", line 1, in <module>
import Persistence.Image as img
ImportError: No module named Persistence.Image
I've been searching for this problem here but can't find anything that worked to solve this as the directory tree seems to be correct as you can see on this image:
I'm using ubuntu if it's any use.
Thanks and regards!

Persistence package does not exist in that source tree. There is a "Persistence" directory there, but it is not a package, because it does not contain a __init__.py file.
From the Python documentation:
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.

I don't believe that you are importing with proper syntax. You need to use from Persistance import Image as img. For example:
>>> import cmath.sqrt as c_sqrt
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cmath.sqrt
ImportError: No module named 'cmath.sqrt'; 'cmath' is not a package
>>> from cmath import sqrt as c_sqrt
>>> c_sqrt(-1)
1j

Related

Problem when importing a package from another directory

I want to import the function write_dynamic_data located in utilities.py into the file trigger_utilities.py which is lower in the hierarchy, but this error is showing up. How can I do? I'm avoiding to use sys.path as I've read it's a bag practise. I'm using Python 3.8.9 and I thinks it isn't necessary anymore).
Project stucture:
utilities.py
[processing]
trigger_utilities.py
This is the console output:
matidellatorre#MacBook-Pro-de-Mati myCode % /usr/bin/python3 /Users/matidellatorre/Desktop/Development/myCode/processing/trigger_utilities.py
Traceback (most recent call last):
File `"/Users/matidellatorre/Desktop/Development/myCode/processing/trigger_utilities.py", line 7, in <module>`
from ..utilities import write_dynamic_data
ImportError: attempted relative import with no known parent package
Thanks in advance!

In Python how do I correct this Module Not Found Error

I have the following module structure in my Administrator project folder
StockController.py (in the root project folder)
then in a package called Utilities I have the following two modules
ExcelManipulator.py
StockPurchaseInfo.py
Relevant Code from each module as as follows:
StockController.py
from Utilities.ExcelManipulator
import ExcelManipulatorClass
ExcelManipulator.py
from StockPurchaseInfo import StockInfoClass
class ExcelManipulatorClass:
StockPurchaseInfo.py
class StockInfoClass:
When running the StockController module I get the following error
Traceback (most recent call last):
File "d:\Administrator\StockController.py", line 1, in <module>
from Utilities.ExcelManipulator import ExcelManipulatorClass
File "d:\Administrator\Utilities\ExcelManipulator.py", line 2, in <module>
from StockPurchaseInfo import StockInfoClass
ModuleNotFoundError: No module named 'StockPurchaseInfo'
However when I run the ExcelManipulator.py module I dont get the error (it is able to find the StockPurchaseInfo module) why is this the case?
Do you have an __init__.py in your Utilities directory ?
And in your ExcelManipulator, you've to do this :
from Utilities.StockPurchaseInfo import StockInfoClass (or from .StockPurchaseInfo import StockInfoClass to import relatively)
If you run your programm from the root of your project (and you should do so), modules path are defined from it (from the root of the project), so if you're doing from StockPurchaseInfo import StockInfoClass then python is looking for an StockPurchaseInfo.py in you're root project.

Run module (both as a module and) locally as script with local import

I am pretty new to python, so I might be missing (and probably am) some critical part of the import system, but I can't for the life of me figure it out at this point. I have a directory structure as follows:
/
/always_run.py
/lib/__init__.py
/lib/data.py
/lib/config.py
File Internals (imports):
/always_run.py
from lib import data
/lib/data.py
from lib import config
yields
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from lib import config as configs
ModuleNotFoundError: No module named 'lib'
Note: I've also tried:
from . import config
yields:
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from . import config as configs
ImportError: cannot import name 'config'
Within data.py I also have:
if __name__ == "__main__":
print("loading myself")
Just to test, but it makes no difference, it never gets to that point
Normally I run my program as python always_run.py and the world is my oyster, but if I try to run data.py directly I always get import failures. (after first cding into the lib directory and running python .\data.py
Is what I am trying to do not possible without adding the local directory to the sys.path, like this(test, works):
import sys
local_path = os.path.dirname(os.path.realpath(__file__))
if local_path not in sys.path:
sys.path.append(local_path)
import config
In the process of writing this, I tested one further thing, cding to the parent directory and running python -m lib.data which works flawlessly. Now my question is one of curiosity, because I still can not find the answer otherwise. Is it not possible to run a local file that is part of a module from the local directory? Or must it be done from another directory?

ImportError: No module named anorm

I got the below shown error. Can anyone please help me with a solution?
from common import anorm, getsize
Exception:
Traceback (most recent call last): File "<pyshell#10>", line 1, in <module>
from common import anorm, getsize
ImportError: cannot import name anorm
Sorry for necroposting, but for anyone who's stuck on this:
apparently, you're trying to run the opencv example somewhat like this one. You need to copy the common.py from the same directory as well and everything would be fine (no need to install common package which has nothing to do with this partucular problem).
The common module does not contain an importable item named anorm. Simple as that.
Some possible reasons why:
The common module was not installed correctly.
Misspelling of the name of the item to be imported.
A file named common.py exists in the current directory, which takes precedence over the real common module (wherever it is).
Circular imports (which don't appear to be the case here, given the error traceback).

Couldn't find cython libraries

I found the following problem in executing my python function:
Traceback (most recent call last):
File "/home/ppd/myfunc.py", line 2, in <module>
from cythonUtilsPy.cythonUtils import *
ImportError: No module named cythonUtils
How to add this cythonUtils module to my path?
Based on the error message, it looks like cythonUtilsPy is already on your path and was found, but the submodule cythonUtilsPy.cythonUtils was not found. Unless you are importing the wrong cythonUtilsPy, there is no path manipulation you can do to fix this.
You need to track down why cythonUtils is not showing up as a submodule of cythonUtilsPy, if cythonUtils is a directory perhaps it is missing an __init__.py.

Categories

Resources