Search code examples
modelicajmodelica

Unable to import pyjmi module when changing directory


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.


Solution

  • 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.