Windows Python Setup Guide¶
This guide will walk you through the steps necessary to setup the recommended Python and M-Star Pre API environment. We will install the following software –
Python 3.9.13
Visual Studio Code
Visual Studio Code Python Extension
Prerequisites
M-Star CFD installed – See Windows
M-Star CFD license is setup – See Licensing . We recommend that you setup your license using an environment variable. Set
mstar_LICENSE
to your license server, eg.5053@license-server
, or the path to the directory containing your license file, eg.c:\licenses
.
Before you start
Take note of the existing M-Star CFD install location, eg.
C:\Program Files\M-Star CFD
Install Python¶
We are using the 3.9 line of Python distributed by python.org. On Windows, M-Star currently only supports Python 3.9.
Download and run - https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe
Customize installation
Optional Features (leave default) – Next
Advanced Options – Enable Install for all users. Leave rest as default. Note the install location. Click Install
Install Visual Studio Code¶
VS Code is the recommended Python file editor on Windows, however you may use whatever text editor you like. Whatever editor you use, just be aware of which Python installation is being invoked, since many computers will have multiple Python installations and environments.
Download and install https://code.visualstudio.com/
Open VS Code
On the left, click the Extensions tab
Type “python” in the search
Click the first “Python” result
Click “Install”
VS Code should now have the Python extension installed
Verify PYTHONPATH is not set¶
Verify no other programs have set the PYTHONPATH environment variable. This can cause our new python environment to fail. It can also cause issues running M-Star Post.
Click start button, type “Settings”, open the Settings app.
In the search tool, enter “environment”. Click “Edit the system environment variables”
In the System Properties window, click Environment Variables… button
Search the user and system variables for PYTHONPATH. If one is found, Delete the variable and restart the computer. Note that this may cause other python environments to break.
Create pth file¶
This is a text file containing the full absolute path to your M-Star CFD install location. This allows your python interpreter to find the mstar
module at runtime.
Open Notepad
Paste the location of the M-Star CFD installation in this file. By default on windows this is
C:\Program Files\M-Star CFD
File - Save As
Change Save as type: All Files
Save the file as mstar.pth
. Note the location where you saved this file. We will be copying this file to another location later on.
Setup M-Star Python environment¶
Choose one of the below options when setting up your environment.
Virtual Environments Option (recommended)¶
This option will setup your Python environment on a per-project basis. This is useful to keep python environments self-contained.
Open command terminal (Click start button, type “cmd” and click enter)
Navigate to where you will keep your M-Star scripts/projects. Use the “cd” command to change directory.
Create a new directory for your project and change to the new directory
mkdir mytest1
cd mytest1
Create a new python virtual environment called venv
. In your command window run:
"c:\Program Files\Python 3.9.13\python.exe" -m venv venv
You should now have a directory called mytest1
with a sub directory venv
.
Open Windows File Explorer
Navigate to your virtual environment directory venv
Go to the sub-directory venv\Lib\site-packages
.
Copy the previously created mstar.pth
file into this venv\Lib\site-packages
directory.
Test virtual environment in command window
Open command terminal
Navigate to the mytest1
directory
Activate the virtual environment
venv\Scripts\activate
Start python
python
Import mstar
python module
import mstar
Checkout M-Star licenses
mstar.CheckOutLicense()
Load empty model
m = mstar.Load()
Print simulation run time
print (m.GetSimParams().Get("Run Time").Value)
Test virtual environment in VS Code
Open Visual Studio code
File - Open folder. Navigate to the mytest1
directory created above.
File - New File. Click Python File.
In the editor window paste the follow text:
import mstar
mstar.CheckOutLicense()
model = mstar.Load()
model.GetSimParams().Get("Run Time").Value = 11
print (model.GetSimParams().Get("Run Time").Value)
File - Save As. Choose a file name mytest.py
.
Verify that VS Code has detected your virtual environment located in the sub directory venv
. In the lower right corner of the VS Code window, you should see Python
and the version and name of the virtual environment venv
. If not, click on the version to change the selected Python environment path.
Change to the Run and Debug tab on the left side of the screen, and click Run and Debug. Select Python Debugger, and Python File to Debug the currently active Python file.
In the Terminal tab on the bottom of the screen you should see the results of the script printed to the screen.
Global Install Option¶
This option will install the M-Star python module into your global Python installation. The advantage of this approach is its simplicity but has the drawback of being less flexible and prone to issues later on if another program changes your installation or environment. Whenever possible, prefer virtual environments.
In Windows File Explorer:
Navigate to your Python installation, eg. C:\Program Files\Python 3.9.13
Go to the sub-directory Lib\site-packages
.
Copy the previously created mstar.pth
file into this Lib\site-packages
directory.
Troubleshooting¶
- Can I specify my license inside my script?
Yes, use the os.environ to set the
mstar_LICENSE
variable. For example:import os os.environ['mstar_LICENSE] = '5053@licenseserver'
- I am not able to load the mstar module, can I try something else?
Yes, set the path to the M-Star installation at runtime. For example:
import sys sys.path.insert(0, r'c:\path\to\M-star CFD') import mstar