Skip to content

Abaqus 2024 Python Upgrade for Scripters and Non-scripters

Posted by Oskar Smedman

Categories
Home > Blog > Abaqus 2024 Python Upgrade for Scripters and Non-scripters

Table of contents

    One major change in Abaqus 2024 comes with the Python scripting interface, which has been upgraded to Python 3. This contrasts with other recent releases, which use Python 2. Broadly speaking, Python 3 is designed to be more secure and easy to use, with the downside that it is not backwards compatible. To be clear, the Abaqus 2024 Python upgrade means that scripts written in Python 2 will generally not run in Python 3 due to significant syntax changes.

    In this post, we will look at some differences that are worth keeping in mind and how to use the 2to3 tool to (mostly) convert your current scripts to Python 3 automatically.

    Printing

    In Python 2, print is a statement:

    print "Spam"

    In Python 3, print is instead a function:
    print ("Spam")

    Coincidentally, using the Python 3 syntax does work in Python 2. However, it is not recommended to write code with the goal of having it compatible with both versions, since this will lead to contrived code in most other cases – contradicting the goal of Python to be easy to use.

    Division

    In Python 2, division between to integers returns an integer. If we ask previous versions of Abaqus to perform such a division, this is what we will see:
    >>> 5 / 2
    2

    In most practical applications, this behavior is both unexpected and unintuitive; to get the “true” result of the division as a float, at least one of the operands must be replaced with a float. 
    In Python 3, float division is the default behavior even if both operands are integers.

    Note that this not only affects scripting in Abaqus; this change may impact features anywhere Abaqus evaluates Python syntax (list not comprehensive):

    •    Expressions used in *PARAMETER
    •    Expression fields in Abaqus/CAE
    •    Expressions in environment files (such as abaqus_v6.env) – especially if they use customized methods such as onCaeStartup().

    This will affect cases where the division operator / was used to intentionally perform integer division, since it behaves differently between the Python versions. Since the conversion script cannot know the programmer’s intention, this change will not be caught by the conversion script. Luckily, this is likely not a very common use case.

    Strings

    Conceptually, we can distinguish between different types of strings:

    •    Text strings contain messages readable by a human.
    •    Byte strings, on the other hand, are sequences of bytes, i.e. integers between 0 and 255. These are a lower level representation and will usually only come into play in data exchange contexts.

    Python 2 makes no fundamental distinction between text strings and byte strings; these may both be stored in the str class. In the case of text strings, their encoding is unspecified. In Python 3, however, these are fundamentally differentiated by the presence of the str class on one hand, intended for text, and bytes / bytearray, intended for byte sequences.

    This especially has implications when we are reading and parsing data stored as text, so make sure to double-check your encoding settings. To help prepare with the upgrade, a string in Python 2 may be prefixed with b:

    byteString = b'Eggs'

    This has no effect within the script itself, but it communicates to the 2to3 utility which type of string is intended; b-prefixed strings will be converted to bytes objects, whereas strings without the b-prefix will remain in the str class. Therefore, preparing your scripts in this way will help the conversion tool.

    Iterating over dicts

    In Python 2, if we would like to iterate over a non-ordered container (such as a dictionary or repository), we have methods such as keys, values and items, which return a list. The contents of the list reflect the contents now that the method is called; this list is not updated if the contents change.

    In Python 3 introduces a different type of object, named a view, which is returned when calling these methods on the container, as opposed to the lists of Python 2.
    The crucial difference is that the view is linked to the container, meaning that the contents of the view update with changes in the container itself. 
    If we want to preserve the behavior of Python 2, i.e. to take a ‘snapshot’ of the container to be manipulated later, the view needs to be wrapped in a list, and this is precisely what the 2to3 tool will do.

    Imports

    Imports are implemented differently between the two Python releases. Most notably, this affects reloading of modules: If we modify a module that we have already imported into Abaqus/CAE, we need to reload the module to get the changes in effect. 
    Notably, the upgrade affects reloading of modules, due to the import functionality being implemented differently between the Python releases.

    In Python 2, we reload using reload(myModule). In Python 3, reload is no longer builtin, but located within the importlib module. Therefore, the reload function must be imported before reloading is possible:

    from importlib import reload
    reload(myModule)

    Similarly to the other cases, if your scripts contain reload calls, the 2to3 tool converts them to be Python 3-compatible.

    Running the conversion script

    Python’s native conversion library, lib2to3, is already included with Abaqus 2023. An enhanced version, abqPy2to3, is included with Abaqus 2024. In order to perform your scripts, use the Upgrade Scripts tool in Abaqus/CAE (Plug-ins > Abaqus > Upgrade Scripts), or run the following command in a command prompt:

    abaqus python -m abqPy2to3 C:\my_script_folder

    This will convert all scripts in the given folder and its subfolders.

    As a safety measure, make sure to test your converted scripts before running them!
    Read more at 3DEXPERIENCE 3DSwym

    Summary

    Though Python 3 eventually will offer an easier and more secure scripting experience, during the transition we occasionally need to be mindful of plug-ins that may not work, and we need to prepare our own scripts on the way. We can use the 2to3 tool to help us on the way after we prepare it – and after that we need to make sure to test the scripts so that they still work as expected.

    Download Abaqus 2024 

    Simulation Driven Innovation.

    TECHNIA Simulation provides top tier FEA, Non-linear, and Advanced Simulation Software, Training, and Consultancy. Our dedicated team of more than 65 Simulation experts across 16 countries advise and support your innovation with a wealth of specialist knowledge and experience.

    About TECHNIA
    Want to receive more content like this?
    • Related news and articles straight to your inbox
    • Hints, tips & how-tos
    • Thought leadership articles

    VISIT THE KNOWLEDGE HUB

    Helping you find the information you’re looking for. Discover webinars, events, FAQ's, case studies and tutorials.

    VIEW HUB
    © TECHNIA 2023 (Part of the Addnode Group) TECHNIA is certified according to ISO standards 9001:2015, 14001:2015 and 27001:2015 – Quality & Environment