如何导入商业软件提供的Python个模块?

How to import Python modules provided by commercial software?

我想测试 Python2 依赖于商业软件 Abaqus 提供的对象的代码。我可以通过 abaqus python:

启动 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

问题是当我尝试导入 Abaqus 提供的模块 abaqus 时,出现错误:

abaqus module may only be imported in the Abaqus kernel process

我在 /some/path/ 下找到了模块的 .pyc 并尝试按照 here 描述的答案执行编译代码:

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)

但这会产生同样的错误。有什么办法可以在 Abaqus 内核进程 外部 导入模块?

UPD 1

我尝试启动子进程,但无济于事。 我创建了一个文件 my_test_script.py:

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

运行 python my_test_script.py 产生:

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

运行 通过 Abaqus 执行相同的脚本会导致以下错误:

$ 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'

Abaqus 回放文件不包含对我有帮助的任何内容:

# 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

任何输出?

我不确定是否最终理解了您问题的所有细节。例如,您试图通过执行“.pyc”文件中的代码来实现什么(顺便说一句,它包含什么?)?

无论如何,我在这里分享适合我的例程。

  1. 正在创建脚本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. 在您的工作目录中启动子进程(请参阅 subprocess.Popen 的文档以及如何从命令行执行 abaqus 作业,位于“Abaqus 分析用户指南 -> 介绍、空间建模和执行 ->作业执行 -> 执行程序”)。您可以在任何 python 解释器中启动以下代码。
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. 如果代码中的某些内容不起作用,我将打开 Abaqus CAE 并尝试在 Abaqus python 解释器中“逐行”启动代码(在 window).您可以在那里调查不同的对象,以便更好地了解 Abaqus 环境的工作方式。