Setting up Jupyter Notebook for JModelica
Asked Answered
O

2

5

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?

Overabundance answered 1/3, 2018 at 14:29 Comment(0)
E
6

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

Europium answered 1/3, 2018 at 16:4 Comment(2)
This did the trick. This might be unrelated and require a separate post, but could a similar logic be applied to get Spyder working with JModelica?Overabundance
Great! Yes, in general, when ever you need to run JModelica.org you need to run setenv.bat. The reason for this is that it sets several environment variables and adding JModelica.org specific paths to PATH and PYTHONPATH needed for JModelica.org to run.Europium
A
1

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.

Aronarondel answered 2/10, 2019 at 23:2 Comment(2)
Good point, I think my answer was written before 64 bit support was added. If I'm not mistaking there should be a setenv_64.bat as well (I left the project over a year ago, so things might have changed).Europium
@JonS Thanks for the replies. Any idea who is in charge of the JModelica project right now? The forum is also down! I have contacted several people inside Modelon no reply so far. ¯\_(ツ)_/¯Aronarondel

© 2022 - 2024 — McMap. All rights reserved.