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.
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.
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.
I am running a python program called diagnostics.py from a java class called PyInterpreter that intializes a jython PythonInterpreter object and a file to use and can run methods from that python program. My python program looks like this:
import datetime
import psutil
class HeartbeatGenerator:
...
and when I try to run this program, I get the error:
Exception in thread "main" Traceback (most recent call last):
File "../../../eclipse-workspace/Diagnostics/diagnostics.py", line 2, in <module>
import datetime
ImportError: No module named datetime
I have installed datetime and psutil with pip and they are in /usr/local/bin/python3.7/site-packages as well as /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages. I have also copied and pasted the necessary files from that location into my jython.2.5.3/lib/site-packages, and in my PyInterpreter file, I have tried setting my system properties for python.home and python.path as seen:
public PyInterpreter()
{
Properties props = System.getProperties();
props.setProperty("python.home", "/usr/local/lib/python3.7");
props.setProperty("python.path", "/usr/local/lib/python3.7/site-packages");
System.out.print(props);
PythonInterpreter.initialize(System.getProperties(),
props,
new String[0]);
this.interpreter = new PythonInterpreter();
}
but no matter what I set the sys properties to, I still get the same error. Right now it is only on the line to import datetime, but I know if I had import psutil first, it would break on that line too. Any ideas?
I figured it out. I just had to add what needed to be on the sys.path to the PYTHONPATH variable in my bash_profile, so that it would add that to the sys.path variable for jython.
I have two scripts, main and statistics. In one script, main, several functions from the other script, statistics, are called. The code is as follows.
if initial_action == "get_portfolio_statistics":
statistics.get_portfolio_status()
statistics.get_current_securities()
statistics.get_open_trades()
Of course, the functions match as far as calling names go, and my IDE (PyCharm) is not giving any warnings. Tests have, refactor after refactor, turned up the fundamentally same error report:
Traceback (most recent call last):
File "G:/stuff/dev/!projects+repositories/RYZ/main.py", line 2, in <module>
import statistics
File "G:\stuff\dev\!projects+repositories\RYZ\statistics.py", line 1, in
<module>
import main
File "G:\stuff\dev\!projects+repositories\RYZ\main.py", line 12, in <module>
statistics.get_portfolio_status()
AttributeError: module 'statistics' has no attribute 'get_portfolio_status'
There are, however, a few intriguing recurrences that have popped up. First of all, in some cases, for no identifiable reason, the tests check out when the function names are changed, however, the results are not consistent with further tests. Second of all, the use of back/forward slashes in the file paths are not consistent, with a G:/ with the first call, and then G:\ for the two latest calls, though the first and latest calls are referring to the same file.
My question is whether this error is a matter of function naming, a matter of file path inconsistencies, a matter of cross-imports, or due to the fact that the functions being called are not housed in a class.
Project Structure:
ryz server backend (RYZ directory)
- main.py
- statistics.py
Import Structure:
# in main.py
import statistics
# in statistics.py
import main
statistics.py Structure:
<imports...>
def get_portfolio_status():
<code>
def get_current_securities():
<code>
def get_open_trades():
<code>
I'll bet it's because of the cross-import. Try to move anything that statistics.py require to that file and then import into and call from main.py.
Your function naming is normal.
I wouldn't worry about the mix of slashes. However, if you're constructing a path yourself, use the os module:
import os
path = os.path.join(os.getcwd(), 'dir', 'filename')
This will ensure that you get a path suited to your platform.
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.