I am using Imatest IT (v5.0.1) and need to use Imatest IT tests with my Python code, however, my Python code also interfaces with MATLAB’s (2017b) Python API and there seems to be a conflict.
You can see below that simply trying to import ImatestLibrary after importing the matlab.engine results in an exception.
Just importing one or the other causes no problem.
import matlab.engine
from imatest.it import ImatestLibrary
Exception caught during initialization of Python interface. Details: DLL load failed: The specified procedure could not
be found.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\imatest\it.py", line 6, in <module>
import imatest.library
File "C:\Python34\lib\site-packages\imatest\library\__init__.py", line 279, in <module>
_pir.import_cppext()
File "C:\Python34\lib\site-packages\imatest\library\__init__.py", line 272, in import_cppext
self.cppext_handle = importlib.import_module("matlabruntimeforpython" + self.interpreter_version)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed: The specified procedure could not be found.
Is there a way to overcome this issue?
According to the documentation of the Imatest Python interface regarding importing imatest.it:
Behind the scenes, the ImatestLibrary constructor will start up the Matlab MCR Runtime and load all the IT libraries into memory.
This suggests that imatest itself meddles with the state of the MATLAB engine. While the order of imports usually doesn't matter, occasionally certain imports should happen before others for proper initialization, when modules imported later rely on modules imported earlier (for instance, importing matplotlib first and setting a backend before importing pyplot that would use said backend).
So my only suggestion is to try switching the order of imports to see if that helps. If it doesn't you're out of luck (as far as Stack Overflow is concerned): both MATLAB and Imatest are expensive proprietary (and even closed source) products so you should file bug reports to either or both of them, because nobody else will be able to tell if and how the conflict could be fixed.
Which version of python are you using? It's possible it's not one of Matlabs supported versions for 2017b, which are limited. I forget exactly which but I think 2.7, 3.3, 3.4, and possibly 3.5
The workaround to this I used was to create new processes using Python's multiprocessing module. The processes would use either the MATLAB libraries or the Imatest libraries. An extra layer of code but it works.
Related
I am using PyScripter to implement a lesson from the Python Tutorial: https://docs.python.org/3.7/tutorial/modules.html#more-on-modules
However, when I attempt to import my own module quadform.py in a separate code by calling
import quadform
quadform.quad_form(1,1,-6)
I get the error:
Traceback (most recent call last):
File "<module1>", line 1, in <module>
ModuleNotFoundError: No module named 'quadform'
Even more confusing is that when I copy + paste the exact code into a separate module saved in the same location, the import on the second module does works. Since then, I have tried various permutations of syntax, using different IDEs, calling from different modules, calling from the shell, and fiddling with the Path in Environment Variables with only a few successes and seemingly at random.
Can someone explain what might be the issue? At the moment, I cannot reliably call any of my modules without getting the same error. I am a beginner trying to pick up Python (and programming in general) on my own so any help would be greatly appreciated.
Read further down:
https://docs.python.org/3.7/tutorial/modules.html#the-module-search-path
https://docs.python.org/3.7/tutorial/modules.html#packages
https://docs.python.org/3.7/tutorial/modules.html#intra-package-references
If you structure your modules like it says, you should be able to import the right things. sys.path is a list of the folders Python looks in to find packages.
I'm rather new to using python and haven't done really anything to configure python. my professor gave us an autograder.py file that checks our python code for correctness. the autograder.py file imports a file called grader.py which then imports something known as cgi (import cgi). inside this cgi file located at '/usr/lib/python2.7/cgi.py' there is a line of code that says
from operator import attrgetter
this is where the problem occurs. i recieve the following error
Traceback (most recent call last):
File "autograder.py", line 12, in <module>
import grading
File "/home/ggkfox/Documents/164-CSCI/1-Assignment/tutorial/grading.py", line 13, in <module>
import cgi
File "/usr/lib/python2.7/cgi.py", line 37, in <module>
from operator import attrgetter
ImportError: No module named operator
it is my understanding that the operator module should be built in (according to other stack overflow posts). this error only occurs when i use python2.7.12 (as opposed to python3.5). i have had a simular issue importing Tkinter in 2.7 aswell.
also i want to say that i have tried uninstalling python using:
sudo apt-get purge python2.7
but it wont even uninstall for me to even attempt to reinstall. im not using any pipenv or desktopenv.
i dont know what else to say, im very new to python coding.
It is almost a necessity to use a virtualenv for each python project.
A virtualenv creates a dedicated python environment and it saves you from a lot of 'operating system' related issues.
Here, you can find some virtualenv solutions compatible with all Python versions, along with the official python 3.6.4 documentation.
I had a simple app up and running that used the PyGui package. After I decided I had everything working I refactored my code and basically broke it up over multiple files, but I am fairly sure I did not change the location of any files. Now when I try to run my app It gives me the following error:
Traceback (most recent call last):
File "blobedit.py", line 16, in <module>
from GUI import Application, ScrollableView, Document, Window, Cursor, rgb
File "/Users/<myusername>/Desktop/BlobEdit/GUI/__init__.py", line 54, in <module>
raise ImportError("Unable to find an implementation of PyGUI for this installation")
ImportError: Unable to find an implementation of PyGUI for this installation
I have tried moving files around for weeks and can't find the problem. Any help would be much appreciated!
You have a directory GUI and inside it __init__.py, this makes the directory an importable package.
Unfortunately, PyGUI also provides a GUI package. Due to the way Python searches for packages to import, your GUI has come up first and this is what is causing problems.
The simple way to solve it is to rename your GUI directory /Users/Destkop/BlobEdit/GUI to something else.
I'm using py2app to make a wxPython frozen binary that uses matplotlib (whew). The resulting executable is quite large, so I'd like to whittle it down if possible.
One obvious way of doing this is to exclude unneeded matplotlib backends. I've tried running my script with python setup.py py2app --matplotlib-backends - which should compile only backends that are used in the script, as documented here: https://pythonhosted.org/py2app/options.html. The resulting executable is indeed a lot smaller, but it's broken. I also tried python setup.py py2app --matplotlib-backends "WXAgg" which failed the same way. Here is most of the traceback (awkwardly copied from Console, sorry).
import wx
File "wx/__init__.pyc", line 45, in <module>
File "wx/_core.pyc", line 4, in <module>
File "wx/_core_.pyc", line 14, in <module>
File "wx/_core_.pyc", line 10, in __load
ImportError: dlopen(/Users/***/***/***/dist/magic_gui.app/Contents/Resources/lib/python2.7/lib-dynload/wx/_core_.so, 2): Library not loaded: #rpath/lib/libwx_osx_cocoau-3.0.dylib
Referenced from: /Users/***/***/***/dist/magic_gui.app/Contents/Resources/lib/python2.7/lib-dynload/wx/_core_.so
Reason: image not found
Maybe I can explicitly tell it to include libwx_osx_cocoau-3.0.dylib. Or, maybe I need to explicitly include another backend (although I am using wxPython, so the WXAgg backend should be right).
I have two questions: first, is there a way I can include only required matplotlib backends without breaking my program? Second, are there other ways I could/should consider to make my program smaller with py2app?
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed last month.
As a hobby/learning project, I'm writing a parser generator in Python. One of my code files is named "token.py" - which contains a couple of classes for turning plain strings into Token objects. I've just discovered that using the "help()" function from the console in Python raises an error for any module defined in a directory that contains a "token.py".
Here's a way to reproduce the error. Create a new directory with the following files:
/New Folder
main.py
token.py
Leave 'token.py' blank. In main.py, write a simple function - for example:
def test():
pass
Then, in your Python console, import 'main' and call 'help(main.test)' - here's what you'll get:
C:\New Folder>python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import main
>>> help(main.test)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\site.py", line 428, in __call__
import pydoc
File "C:\Python31\lib\pydoc.py", line 55, in <module>
import sys, imp, os, re, inspect, builtins, pkgutil
File "C:\Python31\lib\inspect.py", line 40, in <module>
import tokenize
File "C:\Python31\lib\tokenize.py", line 37, in <module>
COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined
>>>
If you delete the 'token.py' file, help() will behave normally. Both Python 3.1 and Python 2.5 exhibit this behavior.
Is this a known issue? If not, how do I report it?
EDIT:
Several comments state that this behavior isn't a bug. The module that defines "help" imports a module called "token" from Python's standard library. However, Python looks in the application folder before it looks in its library to find modules. In the example above, "help" tries to use my "token.py" instead of Python's, which causes the error.
Since Python is defined to exhibit this behavior, I suppose it isn't a bug. But why do people think that this behavior is acceptable? It implies that adding new modules to Python's library - even without changing existing modules - could break existing applications. It also implies that programmers are expected to have memorized the names of all modules in Python's library - how is that any less ridiculous than expecting programmers to memorize every namespace in .NET or Java? Why don't Python applications get their own namespaces? Why aren't Python standard library modules in their own namespace?
The problem is that your local token.py is being imported by help() instead of Python's actual token.py. This will occur for any number of .py files whose names collide with built-in modules. For example, try creating a pydoc.py file in the CWD and then try help() in Python. The help() function is just a built-in Python function, so it follows the same import path as any other Python code.
Whenever you choose to name your modules in a way that mimics module names defined in Python's standard library, you're fully responsible for whatever happens as a result -- other Python standard library modules are likely to rely on those that you are hiding/overriding, and if your own modules don't carefully mimic the functionality of the modules you're hiding, that's not a bug with Python: it's a bug with your code. Of course, this applies to module token as much as to any other.
If this is happening to you accidentally, and you're looking for a way to check your code for likely bugs or iffy constructs (rather than, as you say, for a way to report a nonexisting bug with the Python standard library), I think tools like pylint may be able to help.
You can search for issues and report new ones here: http://bugs.python.org/.
This isn't a bug with the the help() method. There is a module in the standard library named token.py which is imported by tokenize.py (where the error you're seeing originates from).
from tokenize.py:
from token import *
So tokenize.py expects to have a bunch of variables from the standard library token.py, but since the token.py in your working directory is actually imported they are not present which is causing the NameError (one amoungst many reasons import * shouldn't be used).