honey_badger
honey_badger

Reputation: 530

How to import Python modules provided by commercial software?

I would like to test Python2 code that depends on objects provided by commercial software Abaqus. I can start the Python interpreter that is shipped with Abaqus via abaqus python:

Python 2.7.3 for Abaqus 2018 (default, Sep 9 2017, 03:28:08)

[GCC 4.9.2 20150212 (Red Hat 4.9.2-6)] on linux2

The problem is that when I try to import the module abaqus provided by Abaqus, I get the error:

abaqus module may only be imported in the Abaqus kernel process

I found the .pyc for the module under /some/path/ and tried to follow the answer described here to execute the compiled code:

import marshal

s = open('/some/path/abaqus.pyc', 'rb')
s.seek(8)  # go past first eight bytes
code_obj = marshal.load(s)
exec(code_obj)

But this yields the same error. Is there any way I can import the module outside the Abaqus kernel process?

UPD 1

I tried launching a subprocess, but to no avail. I created a file my_test_script.py:

import subprocess
subprocess.call( ["pytest", "--version"] )

Running python my_test_script.py produces:

This is pytest version 4.6.11, imported from /path/to/virtual/environment/pytest.pyc

Running the same script through Abaqus results in the following error:

$ abaqus cae nogui=my_test_script.py
Abaqus License Manager checked out the following license:
"cae" from Flexnet server licabaq1.lic
<39 out of 40 licenses remain available>.
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/apps/Common/Core/ABAQUS/2018-linux-x86_64/cae/linux_a64/tools/SMApy/python2.7/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/path/to/virtual/environment/bin/pytest", line 8, in <module>
    sys.exit(main())
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 65, in main
    config = _prepareconfig(args, plugins)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 214, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/path/to/virtual/environment/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 789, in pytest_cmdline_parse
    self.parse(args)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 997, in parse
    self._preparse(args, addopts=addopts)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 938, in _preparse
    self._consider_importhook(args)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 886, in _consider_importhook
    self._mark_plugins_for_rewrite(hook)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 908, in _mark_plugins_for_rewrite
    for name in _iter_rewritable_modules(package_files):
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 662, in _iter_rewritable_modules
    package_files = list(package_files)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/_pytest/config/__init__.py", line 904, in <genexpr>
    if any(ep.group == "pytest11" for ep in dist.entry_points)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/importlib_metadata/__init__.py", line 289, in entry_points
    return EntryPoint._from_text(self.read_text('entry_points.txt'))
  File "/path/to/virtual/environment/lib/python2.7/site-packages/importlib_metadata/__init__.py", line 138, in _from_text
    config.read_string(text)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/backports/configparser/__init__.py", line 794, in read_string
    self.read_file(sfile, source)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/backports/configparser/__init__.py", line 789, in read_file
    self._read(f, source)
  File "/path/to/virtual/environment/lib/python2.7/site-packages/backports/configparser/__init__.py", line 1193, in _read
    raise e
backports.configparser.ParsingError: Source contains parsing errors: u'<string>'
    [line  2]: u'\U00660000\U00320000\U00700000\U00790000\U00200000\U003d0000\U00200000\U006e0000\U00750000\U006d0000\U00700000\U00790000\U002e0000\U00660000\U00320000\U00700000\U00790000\U002e0000\U00660000\U00320000\U00700000\U00790000\U00320000\U00650000\U003a0000\U006d0000\U00610000\U00690000\U006e0000\U000a0000\x00\x00\x00\x00w_MapString1DArrayListInt.'
    [line  3]: u'\U00660000\U00320000\U00700000\U00790000\U00320000\U00200000\U003d0000\U00200000\U006e0000\U00770000config\x00c'

The Abaqus replay file does not contain anything helpful for me:

# from driverUtils import executeOnCaeGraphicsStartup
# executeOnCaeGraphicsStartup()
#: Executing "onCaeGraphicsStartup()" in the site directory ...
from abaqus import *
from abaqusConstants import *
session.Viewport(name='Viewport: 1', origin=(1.36719, 1.36719), width=201.25, 
    height=135.625)
session.viewports['Viewport: 1'].makeCurrent()
from driverUtils import executeOnCaeStartup
executeOnCaeStartup()
execfile('my_test_script.py', __main__.__dict__)
print 'RT script done'
#: RT script done

Any takes on the output?

Upvotes: 1

Views: 1307

Answers (1)

Roman Zh.
Roman Zh.

Reputation: 1049

I am not sure that finally understood all details of your problem. For example, what are you trying to achieve by executing the code frome a '.pyc' file (btw what does it contain?)?

Anyway, here I am sharing the routine which works for me.

  1. Creating a script my_abaqus_script.py

Note: the list of imports can vary depending on your needs

from abaqus import *
from abaqusConstants import *
from part import *
from sketch import *

m = mdb.Model(name='myModel')
a = abq_model.rootAssembly

my_sketch = m.ConstrainedSketch(name='my_sketch', sheetSize=24.0)

pivots = [(0., 0.), (0., 1.), (1., 1.), (1., 0.)]
n = len(pivots)
for i in range(n):
    my_sketch.Line(point1=pivot_pts[i], point2=pivot_pts[(i+1) % n])

my_part = m.Part(dimensionality=THREE_D, name='MyPart', type=DEFORMABLE_BODY)
my_part.BaseSolidExtrude(sketch=my_sketch, depth=1.0)

# ...and so on

  1. Launching the subprocess in your working directory (see the documentation of subprocess.Popen and how to execute abaqus job from the command line at the "Abaqus Analysis User's Guide -> Introduction, Spatial Modeling, and Execution -> Job Execution -> Execution procedures"). The following code you launch in any python interpreter.
import subprocess
my_cmd = 'abaqus job=my_abaqus_script analysis cpus=1 interactive'

proc = subprocess.Popen(
   my_cmd,
   cwd=my_working_dir,
   stdout='my_study.log',
   stderr='my_study.err',
   shell=True
)

  1. If something in the code doesn't work, I am opening the Abaqus CAE and trying to launch the code "by line" in the Abaqus python interpreter (at the bottom of the window). You can investigate different objects there for a better understanding of how Abaqus environment is working.

Upvotes: 2

Related Questions