Python Virtual Environments With MASTA
With the backwards compatible API, it is possible to use older versions of mastapy
with MASTA 13.0. However, you may also want to use the latest mastapy
to take advantage of the new features. Since it is not possible to install different versions of the same package in the same Python installation, one solution is to use Python Virtual Environments.
A Python Virtual Environment is essentially a self-contained Python installation where you can maintain a collection of packages for a specific application. This means you could create a virtual environment that contains mastapy 10.2.3
and another that contains mastapy 13.0
.
Creating a Virtual Environment
Creating a virtual environment is easy. First, make sure you have installed Python. To create a virtual environment, run the following:
py -m venv [PATH-TO-VIRTUAL-ENVIRONMENT]
Where PATH-TO-VIRTUAL-ENVIRONMENT
is where you want to create your virtual environment. For instance, if you wanted to create a virtual environment called masta-11
, then you could run:
py -m venv masta-11
This command creates a folder called masta-11
in the current directory. Inside the folder you can find the virtual environment's installed packages in the lib
folder, and the interpreter in the Scripts
folder.
Using the Virtual Environment
Once you have created a virtual environment, you need to "activate" it to use it. Using the example above, we could activate the masta-11
virtual environment by running:
masta-11\Scripts\activate.bat
You should now be in your virtual environment. You can use it like a standard Python installation, installing packages with pip
and running scripts with py
. Once you are done, you can "deactivate" the environment by running:
deactivate
You can find more information on how to use Python Virtual Environments in the official documentation.
Virtual Environments with Visual Studio Code
It is possible to use your virtual environment in Visual Studio Code. Make sure you have a Python file opened to access Python interpreter settings.
With a Python file open in Visual Studio Code, select the name of the Python interpreter in the bottom left. In this example, I have a Python 3.8 interpreter currently selected.
You will see a list of interpreters. It is unlikely that your virtual environment will be listed here however, so select the top option.
You can either enter the path to your interpreter manually or browse for it by selecting the "Find..." option.
The Python interpreter can be found in the
Scripts
directory of your virtual environment. Specifically, it is the file calledpython.exe
. Select this to use it as your Python interpreter in Visual Studio Code.
The virtual environment has now been registered and activated in Visual Studio Code. It will recognise the packages you have installed into the virtual environment.
Virtual Environments in MASTA
It is also possible to use your virtual environment with MASTA. Specifically, you can tell Masta to use the virtual environment's Python interpreter as opposed to a standard Python installation.
Within MASTA, navigate to the Python scripting settings. Here, you can navigate to your virtual environment directory and select it. MASTA will then automatically find the Python interpreter.

When selecting the virtual environment, make sure you select the root directory and not the Scripts
directory.
It is also possible to include system site packages when using the virtual environment. This simply means that packages installed in the home directory (the original Python installation) can also be used.