简体   繁体   English

如何将 Abaqus python 库集成到 PyCharm 托管的项目中

[英]How integrate Abaqus python libraries into a project hosted in PyCharm

There is a similar question regarding the integration of Abaqus specific python libraries into a project hosted in PyDev/Eclipse .关于将 Abaqus 特定的 python 库集成到PyDev/Eclipse中托管的项目中,存在类似的问题 But unfortunately the answers were not compatible with my problem at hand.但不幸的是,答案与我手头的问题不相符。

I am using ABAQUS Version 6.11-2 and the Community Edition of PyCharm 3.1.3 .我正在使用ABAQUS版本 6.11-2 和PyCharm 3.1.3的社区版。 The Abaqus python interpreter resides at the following location on my windows7(64) machine.: Abaqus python 解释器位于我的 windows7(64) 机器上的以下位置:

C:\SIMULIA\Abaqus\6.11-2\Python\Obj\Python.exe
Python 2.6.2 for Abaqus 6.11-2 (r262:71600, Jun 29 2011, 19:23:41) [MSC v.1500 64 bit (AMD64)] on win32

The libraries I need PyCharm to resolve in order to give it's code completion magic a go are residing here - at least that's what I believe them to be.我需要 PyCharm 来解析以便为其提供代码完成魔法 go 的库驻留在此处 - 至少我相信它们是这样的。

C:\SIMULIA\Abaqus\6.11-2\Python\Lib
C:\SIMULIA\Abaqus\6.11-2\Python\Lib\abaqus.pyc
C:\SIMULIA\Abaqus\6.11-2\Python\Lib\abaqusConstants.pyc

Here are the first lines of code of the script I am trying to work on.这是我正在尝试处理的脚本的第一行代码。

from abaqus import *
from abaqusConstants import *
backwardCompatibility.setValues(includeDeprecated=True, reportDeprecated=False)
import sketch
import part

PyCharm marks the abaqus and abaqusConstants import with red underlining. PyCharm 用红色下划线标记 abaqus 和 abaqusConstants 导入。 Showing:显示:

 "Unresolved reference 'abaqus'".

Can someone explain to me how to configure the project in PyCharm so that PyCharm can resolve these imports?有人可以向我解释如何配置 PyCharm 中的项目,以便 PyCharm 可以解析这些导入吗?

Adding the mentioned Python.exe as a Project Interpreter in the Settings Dialog will lead to the following error messagebox saying 'Cannot setup a python SDK at ~path~.在设置对话框中将提到的 Python.exe 添加为项目解释器将导致出现以下错误消息框,提示“无法在 ~path~ 设置 python SDK”。 The SDK seems invalid'. SDK 似乎无效'。

屏幕截图 - 设置对话框

屏幕截图 - ErrorMessageBox

Regards问候

Five years late to the party but this worked for me with Abaqus 2016 and PyCharm 2019.1 Professional on Windows 10: 聚会晚了五年,但这对我来说在Windows 10上使用Abaqus 2016和PyCharm 2019.1 Professional:

  1. Open Abaqus CAE, go to the Kernel Command Line Interface (the >>> icon) and enter the following: 打开Abaqus CAE,转到内核命令行界面( >>>图标)并输入以下内容:
>>> import os
>>> print(os.environ['PYTHONPATH'])
C:\SIMULIA\CAE\2016;C:\SIMULIA\CAE\2016\win_b64;C:\SIMUL ...
  1. Copy the output and make that your system-wide PYTHONPATH environment variable. 复制输出并使其成为系统范围的PYTHONPATH环境变量。 I trimmed a repeat entry and some . 我修剪了一个重复的条目和一些. paths. 路径。

设置系统PYTHONPATH环境变量

  1. Restart PyCharm so it picks up the new PYTHONPATH , go to File/Settings/Project/Project Interpreter, click the Cog icon then Add. 重新启动PyCharm以便它获取新的PYTHONPATH ,转到File / Settings / Project / Project Interpreter,单击Cog图标,然后单击Add。 Choose the System Interpreter option then point it towards the python.exe in the Abaqus bin directory. 选择System Interpreter选项,然后将其指向Abaqus bin目录中的python.exe In my case this was C:\\SIMULIA\\CAE\\2016\\win_b64\\code\\bin\\python.exe . 在我的例子中,这是C:\\SIMULIA\\CAE\\2016\\win_b64\\code\\bin\\python.exe Don't be misled by other ones like C:\\SIMULIA\\CAE\\2016\\win_b64\\tools\\SMApy\\python2.7\\python.exe - they won't work. 不要被C:\\SIMULIA\\CAE\\2016\\win_b64\\tools\\SMApy\\python2.7\\python.exe等其他人误导 - 它们无法正常工作。

This isn't bulletproof - for example, your line from abaqus import * won't work for me - even if I add ABA_PATH to the system path I get ImportError: abaqus module may only be imported in the Abaqus kernel process . 这不是防弹的 - 例如, from abaqus import *行不适合我 - 即使我将ABA_PATH添加到系统路径我得到ImportError: abaqus module may only be imported in the Abaqus kernel process But some debugging and code completion works, for example: 但是一些调试和代码完成工作,例如:

将PyCharm调试器与Abaqus一起使用

在PyCharm中完成Abaqus Python代码

Setting the system-wide path seemed a bit heavy-handed but I wasn't able to get it going any other way. 设置系统范围的路径似乎有点笨拙,但我无法通过任何其他方式。

I'm using abaqus 6.14-4, hopefully helpful for you. 我正在使用abaqus 6.14-4,希望对你有所帮助。 I think why we need PyCharm is because we want to fully use its type checker and other functions. 我想为什么我们需要PyCharm是因为我们想要完全使用它的类型检查器和其他函数。 If we only need a editor, then Abaqus PDE is enough. 如果我们只需要一个编辑器,那么Abaqus PDE就足够了。

In order to achieve this goal, I have been search for abaqus python's source code for long time and couldn't find it. 为了实现这个目标,我一直在搜索abaqus python的源代码,但是找不到它。 Since abaqus only provide the compiled *.pyc files, I use the tool uncompyle6 to decode the *.pyc files and add some functions in it. 由于abaqus仅提供已编译的* .pyc文件,因此我使用uncompyle6工具解码* .pyc文件并在其中添加一些函数。

There is my project: abaqus_pycharm 有我的项目: abaqus_pycharm

  1. register \\SIMULIA\\Abaqus\\6.14-4\\tools\\SMApy\\python2.7\\python.exe as your interpreter (Or you can choose any you want) 注册\\SIMULIA\\Abaqus\\6.14-4\\tools\\SMApy\\python2.7\\python.exe作为您的翻译(或者您可以选择任何你想要的)

  2. copy the files at import-files folder to your site-packages folder 将import-files文件夹中的文件复制到site-packages文件夹

notices that this program used os.system command to run abaqus command line, shown as below: 注意到该程序使用os.system命令运行abaqus命令行,如下所示:

def saveAs(self, pathName):
    if isinstance(self.debug, bool) and self.debug:
        print(pathName)
    if 'ABAQUS_BAT_SETTING' in os.environ.keys():
        self.abaqus_bat_setting = os.environ['ABAQUS_BAT_SETTING']
    if 'ABAQUS_BAT_PATH' in os.environ.keys():
        self.abaqus_bat_path = os.environ['ABAQUS_BAT_PATH']
    os.system(self.abaqus_bat_path + ' cae -' + self.abaqus_bat_setting + ' ' + os.path.abspath(sys.argv[0]))

so we need to set environment like: 所以我们需要设置环境:

environ['ABAQUS_BAT_PATH'] = 'D:\\SIMULIA\\Abaqus\\Commands\\abaqus'
environ['ABAQUS_BAT_SETTING'] = 'noGUI'

and it will run as: 它将运行如下:

D:\SIMULIA\Abaqus\Commands\abaqus.bat -noGUI your_current_working_file.py

I would recommend using the abqpy library in Python: https://abqpy.com我建议使用 Python 中的 abqpy 库: https://abqpy.com

It provides code completion for Abaqus commands in any IDE它为任何 IDE 中的 Abaqus 命令提供代码完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM