I'm experimenting with image processing in python and for two days i was stuck with a problem.
I realized that naming a file to struct.py gave the error "numpy.core.multiarray failed to import" when trying the simple script below:
import numpy as np
k = np.ones((9,9))
print(k)
the same code worked when i created a file with another name.
Now I'm looking for an answer as to why that happened.
I created a test file "struct.py" to try different structuring elements for morphological operations. In all my other test files numpy worked correctly, but for some reason, in that "struct.py" script I got an error saying that "numpy.core.multiarray failed to import". I was so invested into solving that problem that during the solving process i didn't think to test if numpy worked in my other scripts (where they had worked before)
I reinstalled python several times, switched IDE, tried to code locally instead of using a version controller but all of these things seemed pretty unnecessary for me to do because i didn't see how that could be the reason for numpy not to work.
Today i saw that there is a python module called struct, and that me using that name for my script must be the cause of the problem, but I still don't understand why numpy stopped working because of that. Is numpy dependent on the struct module? How could this happen?
Also this is my first time actually posting an issue on stackoverflow, please let me know if i should change the contents of my post or delete unnecessary information or add more.
You must not name your module struct.py because struct module exists as built-in (this is used to serialize/deserialize data to/from memory/disk, with endianness & size management for numeric types)
>>> import struct
>>> struct.__file__
'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python37\\lib\\struct.py'
If you create a file named the same way, if another module tries to import the builtin struct module, it could import yours instead, and hard-to-understand errors may appear.
If I create a struct.py file in my current directory containing just:
print("hellooooo bug")
here what happens when I import numpy:
>>> import numpy
hellooooo bug
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\_distributor_init.py", line 9, in <module>
from ctypes import WinDLL
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 14, in <module>
from struct import calcsize as _calcsize
as you see numpy needs struct. Just don't use that name (or any generic name, without checking if it doesn't exist as built-in first). An indirect variant of Python csv import fails
I pretty sure it is because in module numpy there is a file called pickle.py which contains a line called from struct import pack, unpack since your document is called struct but does not contain pack or unpack it gives an error due to a circular import.
Related
In github there are four py Data which I put on my PyCharm. When I run main.py I get this message:
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/main.py
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/main.py", line 6, in <module>
from data_management.read_csv import *
ModuleNotFoundError: No module named 'data_management'
Here is a screenshots:
Can someone help, what I am doing wrong or how can I fix it?
EDIT (Put folders):
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/main.py
WARNING:root:Failed to import geometry msgs in rigid_transformations.py.
WARNING:root:Failed to import ros dependencies in rigid_transforms.py
WARNING:root:autolab_core not installed as catkin package, RigidTransform ros methods will be unavailable
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/main.py", line 7, in <module>
from visualization.visualize_frame import VisualizationPlot
ModuleNotFoundError: No module named 'visualization.visualize_frame'
EDIT:
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/src/main.py
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/src/main.py", line 7, in <module>
from src.visualization.visualize_frame import VisualizationPlot
File "/Users/Armut/Desktop/High_D/src/visualization/visualize_frame.py", line 10, in <module>
from utils.plot_utils import DiscreteSlider
ModuleNotFoundError: No module named 'utils.plot_utils'
Edit (No errors, but I just get a blank picture):
Edit (I installed matplotlib 3.0.3 and got this):
The issue here is, that it is just a picture. If you can see there are buttons like "next". I should be able to click it so I can track it. But how does it work?
Do the following
from read_csv import *
import visualize_frame as vf
The reason why it was not working for you is because you were importing files that dont exist on your system. When you do from data_management.read_csv import *, what you are telling the Python interpreter to do is to search for a folder called data_management inside you're Coursera folder and get everything from read_csv.py.
This is the same case with visualize_frame. Since you have a flat directory structure, you dont need the folder names. You can directly import the .py files as is.
Another thing to note here is that I personally wouldn't do from read_csv import * because I will be flooding my namespace with a lot of things I probably wont use. I would rather use import read_csv as any_alias_you_like. This way I only fill my namespace with what I want by doing the following
x = any_alias_you_like.function_call()
The reason why I didn't do this with the main code solution is because I am not sure where all you are using read_csv functions and classes in your code and if that is not accounted for by prefxing the alias name properly, you will run into a multiple errors. So my advice is to identify all the funcutions/classes that you are using in read_csv.py and prefix them properly with an alias.
I also used the import statement for the visualize_frame differently. This is because, when you do a from import..., you are only partially initializing the module. However, a proper import visualize_frame will ensure that your entire module is initialized in one call and you can use everything it offers by simply prefixing the alias.
Read about the difference between from import and import... here.
Read about how Python searches for libraries here.
When running the following code, I get an error message.
import fuzzywuzzy
print(fuzzywuzzy.fuzz.ratio('about', 'doubt'))
Error message:
Traceback (most recent call last):
File "C:/Users/vincent/Documents/PythonScripts/test2.py", line 2, in <module>
print(fuzzywuzzy.fuzz.ratio('about', 'doubt'))
AttributeError: module 'fuzzywuzzy' has no attribute 'fuzz'
Howerer, the following runs perfectly.
from fuzzywuzzy import fuzz
print(fuzz.ratio('about', 'doubt'))
Could someone help me solve this? I really appreciate it.
The answer to this depends on how your packages are laid out. I'm assuming that your directory tree looks something like this:
fuzzywuzzy/
__init__.py
fuzz.py
In that case, it's likely that fuzz is not imported in the __init__.py for fuzzywuzzy. When importing the top level of a module python only allows access to those things are imported into that module. Sub-modules require additional imports, as you've seen in your example.
If you want the first import to work then you'll need to add import fuzz to the __init__.py file for fuzzywuzzy, otherwise you'll have to use a from ... import. You could also import fuzzywuzzy.fuzz.
I would recommend not adding those imports to __init__.py, however, because it makes your code less explicit an means that new people reading the code will have to look through another file to understand from where functions are being imported.
I have a module called imtools.py that contains the following function:
import os
def get_imlist(path):
return[os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
When I attempt to call the function get_imlist from the console using import imtools and imtools.get_imlist(path), I receive the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\...\PycharmProjects\first\imtools.py", line 5, in get_imlist
NameError: name 'os' is not defined
I'm new at Python and I must be missing something simple here, but cannot figure this out. If I define the function at the console it works fine. The specific history of this module script is as follows: initially it was written without the import os statement, then after seeing the error above the import os statement was added to the script and it was re-saved. The same console session was used to run the script before and after saving.
Based on small hints, I'm going to guess that your code didn't originally have the import os line in it but you corrected this in the source and re-imported the file.
The problem is that Python caches modules. If you import more than once, each time you get back the same module - it isn't re-read. The mistake you had when you did the first import will persist.
To re-import the imtools.py file after editing, you must use reload(imtools).
Same problem is with me I am also trying to follow the book of Programming Computer Vision with Python by Jan Erik Solem" [http://programmingcomputervision.com/]. I tried to explore on internet to see the problem but I did not find any valuable solution but I have solved this problem by my own effort.
First you just need to place the 'imtools.py' into the parent folder of where your Python is installed like C:\Python so place the file into that destination and type the following command:
from PIL import Image
from numpy import *
from imtools import *
Instead of typing the code with imtools.get_imlist() you just to remove the imtools from the code like:
get_imlist()
This may solve your problem as I had found my solution by the same technique I used.
I'm trying to develop a simulation class that replaces serial at specific apps(Win7, python 2.7).
I have a SerialHandle class that works in number of apps, It's job is add logic to the well known serial methods, the idea was to replace python serial.py with a dummy file with the same name so we won't have to change and imports at SerialHandle.
Now i have this file with Serial class just like the origin and it works fine:
serial.py
...Serial()
Since i want to really simulate the methods i need the SerialException from serialutil
so inside my serial.py i'm trying to import it using:
from serial import SerialException
But as expected i'll get this raise since from serial goes to the local file at first:
Traceback (most recent call last):
File "C:/CROW/ATE/DUTDrivers/DD_SimulatorExample/DD_SimulatorExample.py", line 18, in <module>
from Utilities.Serial.SerialHandle.trunk.SerialHandle import SerialHandle
File "C:\CROW\ATE\Utilities\Serial\SerialHandle\trunk\__init__.py", line 4, in <module>
from Utilities.Simulator import serial
File "C:\CROW\ATE\Utilities\Simulator\serial.py", line 11, in <module>
from serial import SerialException
ImportError: cannot import name SerialException
I understand the problem is the file name since at any other file it will work...
I've tried sys.append(site-packages....serial.py) no luck.
Questions:
Any way to tell the interpreter to ignore the local file at a specific from..import?
Is there any other way to import from an absolute path?
Notes:
the file naming as serial.py is not a decision it's a definition so changing the name is not relevant...
Overloading python serial is not an option also...
You must be using python 2.x, since absolute imports are the default in python 3.x. You can use absolute imports in your serial.py file by adding this at the top of the file:
from __future__ import absolute_import
Note that you will need to convert any implicit relative imports from your serial.py file into explicit relative imports. So if you were importing some_func from other_file.py, which is in the same directory, you would need to change that to:
from .other_file import some_func
Note that the "." indicates a relative import from the same package as the current file. See here for additional detail.
I am getting back into python and I'm having a really basic issue....
My source has the following...
def calrounds(rounds):
print rounds
When I run this through the shell and try to call calrounds(3) I get..
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
calrounds(3)
NameError: name 'calrounds' is not defined
Its been awhile since I've used python, humor me.
Did you import your source first?
It says that the first line of your program is calling calrounds with a parameter 3. Move that below your function definition. The definition needs to be before you call the function. If you are using python 3.0+ you need parenthesis for the print statement.
>>> def calrounds(rounds):
print(rounds)
>>> calrounds(3)
3
The first thing to do is to look at how you're calling the function. Assuming it's in myModule.py, did you import myModule or did you from myModule import calrounds? If you used the first one, you need to call it as myModule.calrounds().
Next thing I would do is to make sure that you're restarting your interpreter. If you have imported a module, importing it again will not reload the source, but use what is already in memory.
The next posibility is that you're importing a file other than the one you think you are. You might be in a different directory or loading something from the standard library. After you import myModule you should print myModule.__file__ and see if it is the file you think you're working on. After 20 years of programming, I still find myself doing this about once a year and it's incredibly frustrating.
Finally, there's the chance that Python is just acting up. Next to your myModule.py there will be a myModule.pyc - this is where Python puts the compiled code so it can load modules faster. Normally it's smart enough to tell if your source has been modified but, occassionally, it fails. Delete your .pyc file and restart the interpreter.