python-3.xfmijmodelicapyfmi

How to combine Python 3 with standard JModelica dependent on Python 2?


I would like to install Python 3 with PyFMI at my Windows computer where I already have JModelica 2.10 with Python 2. My idea is thus to compile Modelica models to FMUs in Python 2 as necessary but then develop scripts in Python 3 to run the FMUs and visualise results. How do I best do this to avoid any conflict between the two Python environments?


Solution

  • JModelica 2.10 and Python 2 are already installed using the binary standard installation file for JModelica to Windows. It seems this gives a confined Python environment. Here conda is not used, but pip is included.

    I have made a test installation with Python 3 and PyFMI using Miniconda which seems actually to work and I would like to have comments on if here are potential problems to check or can be better done. I have not taken any steps to remove libraries from Python 2 related to PyFMI from the JModelica installation. I guess that can wait.

    I have done the following in Windows 10:

    1. Standard Windows-installer for JModelica 2.10 done some time ago.
    2. Install Python 3 environment “on top” using conda in the following way:

      a. Download Miniconda for Python3 from here https://docs.conda.io/en/latest/miniconda.html

      b. Install Miniconda3 and with that you get Python 3.x and some packages - but be careful to at early stage in installation and make the choice NOT to have the installation in any “path”. There are two boxes and I choose to leave both boxes un-marked.

      c. Can be good to first update conda by the command:

      $conda update conda
      

      d. Then I create a special environment "pyfmi" with Python 3 for further PyFMI installation by the command

      $conda create -n pyfmi python=3
      

      e. Then activate that environment by the command

      $conda activate pyfmi
      
    3. Now install PyFMI in this active conda environment "pyfmi" just created and some other useful libraries:

      $conda config --add channels conda-forge
      $conda install pyfmi
      $conda install matplotlib
      $conda install ipython
      $conda install jupyter
      
    4. You can now deactivate the "pyfmi" environment and close the command window

    5. In the Windows start menu you now find a "button" for Anaconda PowerShell (and there is another button for Anaconda too). Use the first one to start-up a command-window and from here you the do

      $conda activate pyfmi
      $ipython --pylab
      
    6. Now you can load previous compiled FMU and run it as usual but now in Python 3 environment and the commands are:

      $from pyfmi import load_fmu
      $model=load_fmu(FMU_model)
      
    7. If you like to modify the model and re-compile to get a new FMU you just open a new command window for JModelica/pylab from the Windows start menu as usual. This means you in one command-window work in Python 2 with the full JModelica 2.10 and in the other command-window with Python 3 with PyFMI only. And you can operate on the same folder from both, if you like, but can also be better to have separate folders.

    As said above this installation provides the possibility to run the same FMU both in Python2 with the JModelica command-window or in Python3 with pyfmi command- window.

    I have tested it with a few scripts and used model.simulate() as well as model.estimate() and found no problems, so far.

    Related post is PyFMI in Python 3 environment in Ubuntu 18.04