Skip to main content

Installing PyAnsys modules in a Virtual Environment

| 04.15.2022

Welcome to another Script Tip Friday! This week's Script Tip is brought to you by Sandeep Medikonda, a Lead Consulting Engineer at Ansys.

 

Introduction

"If you haven't heard about the PyAnsys project already. Checkout this article by our very own Pierre Thieffry covering the highlights. In short, the PyAnsys initiative is a collection of Python packages that facilitates the usage of Ansys products through Python (CPython in particular).

In todays article, lets talk about the importance of installation PyAnsys packages inside a virtual environment.

Why Virtual Environments?

One of the many reasons why python is such a popular scripting language is due its vast eco-system of widely accepted libraries/packages with consistent notations. This facilitates easy exchange of information between domains and increases interoperability. However, Python, like most other modern programming languages, also has its own unique way for downloading, storing and resolving packages (or modules). While this has its advantages, the way that the packages are stored on a user's machine and more importantly the way their versioning is handled can lead to some problems. In general, Python doesn't differentiate between versions for 3rd party modules (also known as site packages). Additionally, if different projects you are working on require different versions of a package/module, this could be problematic as well.

In this context, virtual environments offer a great way to create isolated environments to work on Python projects which can utilize their own independent packages. 

Assumptions/Pre-requisites

  • Our focus in this article will be on Python 3 in Windows. However, a similar procedure should work on Linux on as well.
  • One can either install their own version of python directly from python.org or use the the python executable that comes pre-packaged with every Ansys release. For example, in Ansys v2022R1, Python 3.7 is shipped directly and can be found at one of the following locations:

Ansys Structures Installation (assuming the default installation location hasn't changed):

C:\Program Files\ANSYS Inc\v221\commonfiles\CPython\3_7\winx64\Release\python

Ansys Electronics Desktop (AEDT) Installation (assuming the default installation location hasn't changed):

C:\Program Files\AnsysEM\v221\Win64\commonfiles\CPython\3_7\winx64\Release\python

Which ever version of Python 3 is being used. It is important to make sure that the corresponding path is added to the ‘Path’ Environment variable and moved to the top as shown here:

Placeholder

  • There are different ways to create virtual environments in python however, in this article we will only be using the default venv tool that comes packaged with the default python install.
  • It is assumed and expected that the Structures or Electronics package (latest version) of Ansys is already installed to make use of the PyAnsys modules such as PyMAPDL or PyDPF and PyAEDT respectively. If not, please visit the Ansys Customer Portal to download them:

Placeholder

Creating and using Virtual Environments

One advantage of using virtual environments is that there are no limits to the number of environments you can have (well, expect maybe for hard disk space), since they’re just directories containing a few files. It is however recommended to create a virtual environment in a folder where a user has complete read and write permissions on the machine. The %APPDATA% or C:\Users\<username>\AppData\Roaming folder on your Windows machine is typically one of the those locations.

We can create a virtual environment here from the command prompt (cmd.exe) on a windows machine using the following command:

C:\Program Files\ANSYSInc\v221\commonfiles\CPython\3_7\winx64\Release\python\python.exe -m venv "%APPDATA%\PyAnsysEnv"

Once the virtual environment is created, it can be activated by using the following command (on Windows)

call "%APPDATA%\PyAnsysEnv\Scripts\activate"

This should result in the environment being activated and the name echoed within brackets in the command prompt as shown here:

Placeholder

Python package manager (pip) and Installing PyAnsys Modules

A package manager is a tool that allows you to manage the dependencies of your project that are not a part of the Python standard library. There are several package-management tools such as condapipenvpoetry etc. to install, version control and dependency manage python packages with their own set of advantages and dis-advantages. However, in this article, we will only discuss the usage of the default package manager pip which comes pre-packaged with every python install. pip connects to an online repository of public packages, called the Python Package Index (PyPi) which helps one find and install software developed and shared by the Python community.

In the activated virtual environment (which is PyAnsysEnv in our case), it is typically preferred to have pip upgraded to the latest version using the following command:

"%APPDATA%\PyAnsysEnv\Scripts\python.exe" -m pip install --upgrade pip

Next, all PyAnsys modules (and really any popular python module such as Numpy, Matplotlib etc.) can be easily installed using the following commands:

"%APPDATA%\PyAnsysEnv\Scripts\pip" install ansys-mapdl-core
"%APPDATA%\PyAnsysEnv\Scripts\pip" install ansys-mapdl-reader
"%APPDATA%\PyAnsysEnv\Scripts\pip" install ansys-dpf-core
"%APPDATA%\PyAnsysEnv\Scripts\pip" install ansys-dpf-post
"%APPDATA%\PyAnsysEnv\Scripts\pip" install pyaedt

There are several Integrated Development Environments (IDE's) where this virtual environment can be invoked from, such as Microsoft Visual Studio Code, PyCharm etc. However, 2 popular open-source IDE's also available as packages from PyPi are Jupyter Lab and Spyder, these can also be easily installed using the following commands... (Please note that all these commands can also be neatly packaged in a batch script and run quite easily by double clicking on it, as discussed in the PyAEDT docs (bat file).

"%APPDATA%\PyAnsysEnv\Scripts\pip" install jupyterlab
"%APPDATA%\PyAnsysEnv\Scripts\pip" install spyder

TL;DR

  • Python Virtual Environments create isolated environments to work on Python projects and will eliminate a lot of typical installation issues one might face. This applies for PyAnsys projects as well.
  • Use of a package manager such as pip inside this virtual environment helps install, version control and dependency manage python packages effectively.
  • To install PyAnsys modules on a local machine, the following simple 3-step process can be followed:
    • Install Ansys products of choice (Structures or Electronics or both).
    • Install a Python version of choice or re-use the Python executable provided in the Ansys installation.
    • Install the PyAnsys modules (PyMAPDL, PyDPF or PyAEDT) as well as an IDE (Spyder, Jupyter, Jupyter Lab, …) or a python module of your choice using pip."