4
votes

I would like to use Jupyter Notebook for editing JModelica python scripts; however after installing, the Jupyter notebook cannot locate key libraries, such as pymodelica.

Here is the process I followed to install Jupyter Notebook for JModelica Version 2.1:

  1. In the IPython terminal: !pip install jupyter

  2. In windows CMD I navigate using cd C:\JModelica.org-2.1\Python27\Scripts

  3. I then open using jupyter notebook

  4. Within the notebook I create a Python 2 file and attempt to import the pymodelica library, which is followed by:

ImportError Traceback (most recent call last) in () ----> 1 import pymodelica

ImportError: No module named pymodelica

Presumably the Jupyter Notebook is running from the Python Kernal installed with JModelica, in which case why is Jupyter Notebook unable to locate the pymodelica library? Are there any settings that need to be changed in order for Jupyter to load these libraries?

2

2 Answers

5
votes

You need to instruct Python where to find the JModelica.org specific Python packages. The easiest way to do this is to run C:\JModelica.org-2.1\setenv.bat before starting jupyter

1
votes

I addition to what @Jon S mentioned, I want to add that you need to set the environment to 64bit version if you get the error messages like

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-907558296850> in <module>()
----> 1 import pyfmi

C:\JModelica.org-2.10\install\Python\pyfmi\__init__.py in <module>()
     22 
     23 #Import the model class allowing for users to type: from pyfmi import FMUModel
---> 24 from .fmi import FMUModel, load_fmu, FMUModelME1, FMUModelME2
     25 from .fmi import FMUModelCS1, FMUModelCS2
     26 from .fmi_coupled import CoupledFMUModelME2

ImportError: DLL load failed: %1 is not a valid Win32 application.

upon importing any of the JModelica packages. To do so:

  1. open a Windows terminal (cmd, PowerShell, ConEmu...)
  2. navigate to the JModelica installation folder (e.g., cd C:\JModelica.org-2.10 or C:\Users\<user>\AppData\Roaming\JModelica.org-2.10 if you install with Chocolatey)
  3. run setenv.bat 64

Now you should be able to import all JModelica packages.

P.S. For others interested to learn and know more about JModelica here I have written a small toturial and collected many usefull refrences.