compiled Python3 module produces error "dynamic module does not define module export function" - python-3.7

I am trying to compile a Python package that I recently migrated from Python2 to Python3.
When running the source code in Python3 it works as expected, so do the source and compiled versions for Python2, but when I cythonize and compile the Python3 package the resulting binaries throw this error when importing a certain module:
dynamic module does not define module export function (PyInit_NKPD)
EDIT:
When I then close the Python interpreter, open it again and import the same model it works.
Interestingly when I put the code on my local drive it imports fine, but when I put it on my server and import it from there I get the above error. And to make matters worse, sometimes it imports fine from the server as well if I just wait a while and try again:
lockjaw:controller frank$ python3.7
Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
NukepediaDB imported
trying to import model.NKPD...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/controller/common.py", line 15, in init controller.common
import model.NKPD # this does not import
ImportError: dynamic module does not define module export function (PyInit_NKPD)
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
trying to import model.NKPD...
NKPD imported
model.NKPD always seems to import fine, but model.NKPD seems to be the stumbling block, even though this may be a red herring. It almost behaves like sometimes the server connection drops out after the first import, though in that case the other incarnations (Python 2/source code) would throw errors as well (not to mention a whole lot of other things would break in my office).
I have reduced the package to three modules with nothing but import and print statements which can be found here (including the resuling C++ and binary files).
The structure is very simple:
Where common is the module I import to test by cd-ing into the src/controller folder, opening Python3.7 and importing common. Common then imports model.NukepediaDB and model.NKPD, the latter causing the trouble.
I cythonized the python files with this line:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 py2cpp.py
Then compiled with this:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 setup.py build_ext --inplace
I would love to understand why the import sometimes fails when run from the server, as I have to distribute this package to others and need to ensure it runs reliably.
Thanks,
Frank

Related

ModuleNotFoundError: No module named 'windows'

I'm working on a project and I need to use the PyMouse module.
pip install pymouse
installed pymouse correctly, so I assumed all was fine.
However, when importing PyMouse:
from pymouse import PyMouse
I got the following error running my program:
Traceback (most recent call last):
File "4opeenrij.py", line 1, in <module>
from pymouse import PyMouseEvent
File "C:\Users\lcdew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymouse\__init__.py", line 92, in <module>
from windows import PyMouse, PyMouseEvent
ModuleNotFoundError: No module named 'windows'
I can't seem to figure out what might cause this error message. Any help would be much appreciated.
Additional info:
I'm using Python 3.7 32 bit
Current pip version: 18.1
I have Windows 10
working on a 64-bit operating system
I had I look into this and became puzzled at first, so looked deeper.
It turns out that pymouse is absolutely full of errors. More that I bothered to find.
The error you got is just one of many errors caused by bad coding.
The code says:
from windows import PyMouse, PyMouseEvent
And it should say:
from .windows import PyMouse, PyMouseEvent
Also, PyUserInput, a sister package that is free from the pymouse errors, requires pyhook, which is unsupported by python 3. After a lot of looking around, the conclusion that there is no way around the problems found, except maybe installing a really early version.
You could also try the keyboard module.
You might want to take a look at pynput module. It works on python 3.8, doesn't have any incompatible dependencies, and doesn't seem to have any errors. Once you have the module installed, this page gives some good examples of various ways to manage the mouse.

ImportError: when importing from a local script library

Does importing from a local script lib work in qpython3?
I have a script I created in the Qpython projects3 directory of qpython.
projects3/MPU6502/
I have my main code here in this directory.
main.py
I have a local subdirectory,
projects3/MPU6502/tiny6502lib/
In this sub directory I have the source code,
RAM_8bit_Memory.py
From my main.py source code I load the library,
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
# import 6502 library module
from tiny6502lib.RAM_8bit_Memory import *
when I execute the library import I get the ERRORL:
"ImportError: No module named tiny6502lib.RAM_8bit_Memory"
Is importing local script libraries broken in qpython3?
org.qpython.py/projects/MPU6502/main.py
org.qpython.py/projects/MPU6502/tiny6502lib/RAM_8bit_Memory.py
This code works perfectly on all other flavors of python3 (windows) and pythonista on ipad.
How do I get this import to work? Or does it not work at all?
thank you,
Just try to insert the following code before importing tiny6502lib.RAM_8bit_Memory
import sys
sys.path.append("/sdcard/qpython/projects3/MPU6502")
?

Scipy and CX_freeze - Error importing scipy: you cannot import scipy while being in scipy source directory

I'm having trouble compiling an exe while using cx_freeze and scipy. In particular, my script uses
from scipy.interpolate import griddata
The build process seems to complete successfully, however when I try to run the compiled exe, I get the following message.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "gis_helper.py", line 13, in <module>
File "C:\Python27\lib\site-packages\scipy\__init__.py", line 103, in <module>
raise ImportError(msg)
ImportError: Error importing scipy: you cannot import scipy while
being in scipy source directory; please exit the scipy source
tree first, and relaunch your python intepreter.
After looking at scipy\ _init__.py file, there is the following:
if __SCIPY_SETUP__:
import sys as _sys
_sys.stderr.write('Running from scipy source directory.\n')
del _sys
else:
try:
from scipy.__config__ import show as show_config
except ImportError:
msg = """Error importing scipy: you cannot import scipy while
being in scipy source directory; please exit the scipy source
tree first, and relaunch your python intepreter."""
raise ImportError(msg)
I'm not entirely sure what is the problem here however although it seems that the erros is being thrown because there is a problem with the scipy config file. Possibly not being included in the build process. I'm quite a novice and hoping someone more experienced with generating build using cxfreeze can shed some light on this.
Incidentally, the scipy in use was installed from binaries here.
i have had the same issue. I added this code to the setup.py generated by cx_freeze:
import scipy
includefiles_list=[]
scipy_path = dirname(scipy.__file__)
includefiles_list.append(scipy_path)
Then, used includefiles_list as part of the build_exe parameter:
build_options = dict(packages=[], include_files=includefiles_list)
setup(name="foo", options=dict(build_exe=build_options))
I add the same problem and solved it using fepzzz method and including some missing packages:
additional_mods = ['numpy.matlib', 'multiprocessing.process']
includefiles = [(r'C:\Anaconda3\Lib\site-packages\scipy')]
setup(xxx, options={'build_exe': {'includes': additional_mods, 'include_files': includefiles}})
And using version 5.0.2 of cx-Freeze package, which solved an error when importing multiprocessing.process

Virtualenv PyDev Undefined variable from import error

First of all I'm aware of the question here but I couldn't find a satisfied answer there. I don't want to ignore errors or use comments - I want to have the right settings in eclipse/pydev. My problem is pretty similar to this one.
I'm using Ubuntu 12.04 and installed a virtuenv for python 2.7 in my home directory. After installing several python packages (numpy, scipy, matplotlib, etc.) using pip, I installed eclipse 4.3 with pydev.
If I use the python system interpreter at /usr/bin/python everything works fine (except that I didn't want to use). However, if I try to set up a python interpreter using the virtualenv first I get this warning describe here. After clicking "proceed anyway", it seems to work. So far so good.
However e.g. import numpy as np gives for each np.* call the eclipse/pydev error Undefined variable from import, also the code completion doesn't work properly. It seems to work, e.g. for datetime, but not for numpy, scipy and matplotlib.
Does anybody figured out to configure eclipse correctly?
I already tried to add the numpy path manually to the virtualenv interpreter, but than I get the weird error:
import matplotlib.dates as mpl_dates
File "/home/pydev/myenv-py27/local/lib/python2.7/site-packages/matplotlib/init.py", line 149, in
import sys, os, tempfile
File "/usr/lib/python2.7/tempfile.py", line 34, in
from random import Random as _Random
ImportError: cannot import name Random

Import issues - is my Jython installed correctly?

I'm starting using Jython. I noted that sys.path is completely different when Jython is executed from the command line than from Eclipse.
Command line
tk:~$ jython
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[Java HotSpot(TM) Server VM (Oracle Corporation)] on java1.7.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['',
'/jproject/extras/2.5.3/Lib',
'__classpath__',
'__pyclasspath__/',
'/jproject/extras/2.5.3/Lib/site-packages']
It's OK, I can import everything from there (import pdb, import csv, etc).
Eclipse
I added jython.jar in Eclipse to the Java Build Path using the "Add JARs" button. I wrote a simple Java class (excerpt):
PythonInterpreter pi = new PythonInterpreter();
pi.exec("import sys");
pi.exec("print sys.path");
Output:
['/jproject/projects/foobar/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProject/WEB-INF/lib/Lib',
'__classpath__',
'__pyclasspath__/']
From Python scripts created in the Java project I can't import anything. I always get ImportError: No module named ...
So I created a file called .jython in my home directory containing this:
python.path=/jproject/extras/2.5.3/Lib:/jproject/extras/2.5.3/Lib/site-packages
It seems to fix the import issue. But, is this the right way to proceed?
I think I don't have to do the last step manually and probably I installed Jython badly.
Looks like I have same issue described in my question here:
The solution I've come up is workaround that doesn't require any actions from user to set python.path: Basically I added code that sets python.path to application work directory (user.path) before initializing jython environment.