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



我想测试依赖于商业软件Abaqus提供的对象的Python2代码。我可以通过abaqus python:启动Abaqus附带的Python解释器

Python 2.7.3 for Abaqus 2018(默认,2017年9月9日,03:28:08(

[GCC 4.9.2 20150212(Red Hat 4.9.2-6(]在linux2 上

问题是,当我试图导入Abaqus提供的模块abaqus时,我得到了错误:

abaqus模块只能在abaqus内核进程中导入

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

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'U00660000U00320000U00700000U00790000U00200000U003d0000U00200000U006e0000U00750000U006d0000U00700000U00790000U002e0000U00660000U00320000U00700000U00790000U002e0000U00660000U00320000U00700000U00790000U00320000U00650000U003a0000U006d0000U00610000U00690000U006e0000U000a0000x00x00x00x00w_MapString1DArrayListInt.'
[line  3]: u'U00660000U00320000U00700000U00790000U00320000U00200000U003d0000U00200000U006e0000U00770000configx00c'

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

注意:进口清单可能因您的需求而异

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 Analysis User’s Guide"-"Introduction,Spatial Modeling,and Execution"-"job Execution";执行程序"(。以下代码可以在任何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解释器中(位于窗口底部(。您可以在那里调查不同的对象,以便更好地了解Abaqus环境是如何工作的

最新更新