Unable to import pyjmi module when changing directory - modelica

I am using JModelica 2.1 on Windows, and up until recently I was always able to import pyjmi without any issue. For example, in the IPython console, I would often run the following without issue:
cd C:\mydirectory
from pyjmi import *
However, recently, I accidentally opened a new IPython console and ran the previous code segment without realizing that I already had one opened which was running an optimization problem. Ever since then, the previous code will throw the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-ecb3494d14c4> in <module>()
----> 1 from pyjmi import *
C:\JModelica.org-2.1\install\Python\pyjmi\symbolic_elimination.py in <module>()
21 import scipy
22 import casadi
---> 23 import modelicacasadi_wrapper as ci
24 import itertools
25 from collections import OrderedDict
C:\JModelica.org-2.1\install\Python\modelicacasadi_wrapper\__init__.py in <module>()
----> 1 from modelicacasadi_wrapper import *
C:\JModelica.org-2.1\install\Python\modelicacasadi_wrapper\modelicacasadi_wrapper.py in <module>()
155 SwigPyIterator_swigregister(SwigPyIterator)
156
--> 157 import casadi.casadi_core
158 class Printable(_object):
159 """Proxy of C++ ModelicaCasADi::Printable class."""
ImportError: No module named casadi_core
The curious thing is that the above error only happens when I change the current directory. If instead I don't cd and stay in the default directory (C:\Users\Public\Documents\JModelica.org), then it imports without any problems and works fine afterwards. It only happens with pyjmi (pyfmi for example will import without issue), which seems to suggest that it might be a problem related to environment variables. However, I can't find any problem with them, in all cases the JMODELICA_HOME as well as all other environment variables found with pyjmi.environ seem to work fine.
I tried circumventing the issue by installing JModelica 2.2, however the problem still persists (both with 2.1 and 2.2). It should be noted however that I didn't uninstall JModelica 2.1 when doing this.
If anybody has any idea about what is causing this behaviour, I would be very grateful.
Edit:
Things have taken a turn for the worst, and now I cannot import pyjmi under any circumstances whatsoever when using JModelica 2.1 (whereas the previous behaviour still holds with JModelica 2.2). I don't know what exactly has changed to cause this.

It turns out that the problem was that I had a Python script named casadi.py (and casadi.pyc along with it) in a folder in my C:\ drive. This script was completely unrelated to JModelica's casadi module. The order of the paths in sys.path made it such that, if the current directory wasn't C:\JModelica.org-2.2, it would find the unrelated casadi.pyc before finding the actual module I wanted. To fix the problem I simply renamed the unrelated script, though I imagine that manually changing the contents of sys.path would have done the trick as well.

Related

Distributing pybind11 extension linked to third party libraries

I'm working on a pybind11 extension written in C++ but I'm having a hard time understanding how should it be distributed.
The project links to a number of third party libraries (e.g. libpng, glew etc.).
The project builds fine with CMAKE and it generates a .so file. Now I am not sure what is the right way of installing this extension. The extension seems to work, as if I try copy the file into the python lib directories it is picked up (I can import it, and it works correctly). However, this is clearly not the way to go I think.
I also tried the setuptools route (from https://pybind11.readthedocs.io/en/stable/compiling.html) by creating a setup.py files like this:
import sys
# Available at setup time due to pyproject.toml
from pybind11 import get_cmake_dir
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup
from glob import glob
files = sorted(glob("*.cpp"))
__version__ = "0.0.1"
ext_modules = [
Pybind11Extension("mylib",
files,
# Example: passing in the version to the compiled code
define_macros = [('VERSION_INFO', __version__)],
),
]
setup(
name="mylib",
version=__version__,
author="fab",
author_email="fab#fab",
url="https://github.com/pybind/python_example",
description="mylib",
long_description="",
ext_modules=ext_modules,
extras_require={"test": "pytest"},
cmdclass={"build_ext": build_ext},
zip_safe=False,
python_requires=">=3.7",
)
and now I can build the extension by simply calling
pip3 install
however it looks like all the links are broken because whenever I try importing the extension in Python I get linkage errors, as if setuptools does not link correctly the extension with the 3rd party libs. For instance errors in linking with libpng as in:
>>> import mylib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/fabrizio/.local/lib/python3.8/site-packages/mylib.cpython-38-x86_64-linux-gnu.so: undefined symbol: png_sig_cmp
However I have no clue how to add this link info to setuptools, and don't even know if that's possible (it should be the setuptools equivalent of CMAKE's target_link_libraries).
I am really at a loss after weeks of reading documentation, forum threads and failed attempts. If anyone is able to point me in the right way or to clear some of the fog it would be really appreciated!
Thanks!
Fab
/home/fabrizio/.local/lib/python3.8/site-packages/mylib.cpython-38-x86_64-linux-gnu.so: undefined symbol: png_sig_cmp
This line pretty much says it clearly. Your local shared object file .so can't find the libpng.so against which it is linked.
You can confirm this by running:
ldd /home/fabrizio/.local/lib/python3.8/site-packages/mylib.cpython-38-x86_64-linux-gnu.so
There is no equivalent of target_link_libraries() in setuptools. Because that wouldn't make any sense. The library is already built and you've already linked it. This is your system more or less telling you that it can't find the libraries it needs. And those most likely need to be installed.
This is also one of the reasons why Linux distributions provide their own package managers and why you should use the developer packages provided by said distributions.
So how do you fix this? Well your .so file needs to find the other .so files against which you linked to understand how this works I will refer you to this link.
My main guess is based on the fact that when you manually copy the files it works - That during the build process you probably specify the rpath to a local directory. Hence what you most likely need to do is specify to your setuptools that it needs to copy those files when installing.

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

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

H2OConnectionError: Not connected to a cluster

hi guys recently i've encounter this problem in google colab, and i dont know how to solve this problem
import h2o
h2o.init
data_train = "https://drive.google.com/open?id=14OYzA93IkuKPlkdvXs9Od53HyopgSQ2u"
data_train_df = h2o.import_file(path=data_train)
i following a book that suggest these following codes, but somehow it wont work.
The h2o package is not automatically installed in Google Colab. If you attempt to run import h2o without installing it, you will get the following error.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-accdebc0c7de> in <module>()
----> 1 import h2o
ModuleNotFoundError: No module named 'h2o'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Based on the error message you can then start a new code block and run !pip install h2o to install the package. Then, running import h2o should be successful.
One more thing to keep in mind is that you need to include parentheses to call a function. Running h2o.init will return the description <function h2o.h2o.init>. Try h2o.init() to initialize an h2o cluster.
Edit: The “not connected to a cluster” means you have not initialized a cluster. Try the h2o.init() command again, but using parenthesis so that the function is called and not printed.

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.

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