文章目录

  • 在ANSA中导入外部python库
    • NumPy
    • SciPy
    • OpenSSL
    • H5py
  • PIP 和venv包和环境管理系统
    • PIP
    • venv
  • CONDA包和环境管理系统
    • 下载Conda
    • Conda环境安装和设置
    • 安装库
    • 将库导入到ANSA和META中
    • 公司内联网中的Conda
    • 安装Matplotlib
    • 其他库

在ANSA中导入外部python库

NumPy

NumPy是 Python 用于科学计算的扩展。它支持大型多维数组,并具有大量的高级数学函数库来操作这些数组。

从ANSA 21.0.0 开始, v1.18.2版本的NumPy 库被集成到 BETA 中,它使用的是MKL LP64(32位整数)。用户可以通过导入库来访问:

import numpy
numpy.show_config()

有关 NumPy 的更多信息,可点击此链接获取:NumPy。

SciPy

Scipy 是Python用于科学计算的一个库(library)。其中包含用于优化、线性代数、集成、插值、FFT、信号处理、ODE 求解等模块。

BETA中安装了1.5.1版本的SciPy。

import scipy
from scipy import linalg
import numpy as npdef find_det():arr = np.array([[1, 2],[3, 4]])det_res = linalg.det(arr)print(det_res)

OpenSSL

OpenSSL 是 TLS 和 SSL 协议的工具包库。它也是一个通用的密码库。

BETA中安装的是1.1.1d版本。

H5py

H5py是HDF5二进制数据格式的python接口。

BETA中安装了2.10.0版本的H5py。

PIP 和venv包和环境管理系统

设置 Python 环境是具有挑战性的,因为库可能有多个依赖关系,并且能在特定的Python版本下工作。而包管理系统可以帮助我们解决这些问题。

PIP

pip 是用于安装和管理 Python 包的标准管理系统。pip随着python一起被发布。BETA Python 接口可以在程序中或从命令行访问它。

命令行访问

BETA_CAE_Systems/shared_v21.0.0/python/python.sh -m pip install requests

在程序中访问

import pip
pip.main(['install','requests'])

venv

venv为在自己的站点目录下创建"虚拟环境"提供支持。每个环境都与自己的 Python 二进制文件及其在站点目录中独立安装的 Python包相隔绝。激活所需的虚拟环境时BETA Python接口(interpeter)将从中导入包(packages ),用户无需对导入的包进行管理。

创建新的虚拟环境:

BETA_CAE_Systems/shared_v21.0.0/python/python.sh -m venv /path/to/create/venv/my_test_venv --system-site-packages

激活新环境:

/path/to/create/venv/my_test_venv/activate

激活将使环境中的Python来激活起来。执行的任何pip安装都将应用于此虚拟环境。ANSA 或 META 可以自动找到此环境的路径。

停用环境:

deactivate

CONDA包和环境管理系统

CONDA 是一个包管理系统,使用它可以轻松安装具有大量依赖性的第三方库。CONDA系统的部分库可以导入到ANSA 和 META中使用 。

以下为使用步骤:

下载Conda

下载并安装Miniconda。Miniconda是Anaconda Python发行版的精简版(reduced version)。

Conda环境安装和设置

需要将Conda安装目录添加到系统的环境路径中。

in bash

export PATH="/home/my_name/miniconda3/bin":{$PATH}

in tcsh

setenv PATH $PATH:/home/my_name/miniconda3/bin

在windows:

c:\miniconda3\scripts

Conda的环境是相互独立的目录结构,其允许相同库的多个版本同时存在。ANSA和META自带Python 3.8。因此,我们需要创建一个兼容Python 3.8的库的环境。

conda create --name python38 python=3.8

最后,需要激活新创建的环境,以便新的安装是针对Python 3.8的。

在Linux系统下:

conda activate python38

注意:
只能在bash中完成虚拟环境的激活工作

在Windows系统下:

conda activate python38

安装库

在激活的环境中安装所需的库采用如下方式:

conda install library_name

将库导入到ANSA和META中

为了将Conda环境导入到ANSA或META脚本中,请在系统路径中添加Conda的Python 3.8的site-packages路径。为了获取site-packages的路径,在Conda的python38环境中运行以下命令:

python3 -c "import site; print(site.getsitepackages()[0])"

在ANSA和META中使用返回的路径来导入所需的库。

import sysCONDA_ENV = '/home/my_name/miniconda3/envs/python38/lib/python3.8/site-packages'
sys.path.append(CONDA_ENV)import scipy

公司内联网中的Conda

为了在公司内部网中拥有Python 3.8r的兼容库,在不访问Internet的情况下,需要设置一个Conda Channel。安装了单独的miniconda的用户将能够从本地conda通道(Conda Channel)来安装所需的库。

创建和设置自定义通道的一般指令可以在这里找到。

这里提供了一个python脚本,用于设置conda通道。该脚本在Python 3中运行,需要Internet连接才能下载给定Python版本所需的库。

设置conda通道python脚本:

import sys
import os
import urllib.parse
import urllib.request
import json
import optparseclass CondaRepo():def __init__(self, py_version, is_win, is_linux, is_mac):self.py_version = py_versionself.py_version_float = float(py_version)if self.py_version_float < 3.5:self.main_repos = ['https://repo.anaconda.com/pkgs/free/']else:self.main_repos = ['https://repo.anaconda.com/pkgs/free/','https://repo.anaconda.com/pkgs/main/']self.output_path = ''self.is_win = is_winself.is_linux = is_linuxself.is_mac = is_macself.all_suffixes = ['noarch']if self.is_win:self.all_suffixes.append('win-64')if self.is_linux:self.all_suffixes.append('linux-64')if self.is_mac:self.all_suffixes.append('osx-64')@propertydef output_path(self):return self._output_path@output_path.setterdef output_path(self, output_path):if not output_path:self._output_path = output_pathreturn if not os.path.isdir(output_path):raise OSError('channel path is not valid')if not os.access(output_path, os.W_OK):raise OSError('you do not have permission to write in the channel dir')self._output_path = output_path@propertydef py_version(self):return self._py_version@py_version.setterdef py_version(self, py_version):if not py_version:self._py_version = py_versionversions_dict = {'3.0':'py30','3.1':'py31','3.2':'py32','3.3':'py33','3.4':'py34','3.5':'py35','3.6':'py36','3.7':'py37','3.8':'py38'}self._py_version = versions_dict[py_version]@propertydef is_win(self):return self._is_win@is_win.setterdef is_win(self, is_win):if type(is_win) == bool:self._is_win = is_winreturnbool_dict = {'true':True,'false':False}self._is_win = bool_dict[is_win]@propertydef is_linux(self):return self._is_linux@is_linux.setterdef is_linux(self, is_linux):if type(is_linux) == bool:self._is_linux = is_linuxreturnbool_dict = {'true':True,'false':False}self._is_linux = bool_dict[is_linux]@propertydef is_mac(self):return self._is_mac@is_mac.setterdef is_mac(self, is_mac):if type(is_mac) == bool:self._is_mac = is_macreturnbool_dict = {'true':True,'false':False}self._is_mac = bool_dict[is_mac]def _create_subdir(self, subdir_name):dir_path = os.path.join(self.output_path, subdir_name)if not os.path.exists(dir_path):os.makedirs(dir_path)def _download_libs(self, repos_info, repo, repo_dir):subdir = repos_info['info']['subdir']subdir_tmp  = os.path.join(repo_dir,subdir)self._create_subdir(subdir_tmp)save_path = os.path.join(self.output_path, repo_dir, subdir)def perform_download(subdir, pack_name):pack_name = pack_name.replace('tar.bz2', 'conda')save_path_tmp = os.path.join(save_path, pack_name)if os.path.exists(save_path_tmp):returnprint('downloading : %s - %s ...'%(subdir, pack_name))try:down_path = os.path.join(repo, subdir, pack_name)req = urllib.request.Request(down_path)ret = urllib.request.urlopen(req)except:print('unable to download : %s - %s ...'%(subdir, pack_name))pack_name = pack_name.replace('conda', 'tar.bz2')save_path_tmp = os.path.join(save_path, pack_name)try:print('downloading : %s - %s ...'%(subdir, pack_name))down_path = os.path.join(repo, subdir, pack_name)req = urllib.request.Request(down_path)ret = urllib.request.urlopen(req)except:print('unable to download : %s - %s ...'%(subdir, pack_name))returnwith open(save_path_tmp, 'wb') as f:f.write(ret.read())ret.close()perform_download(subdir, 'repodata.json')perform_download(subdir, 'repodata.json.bz2')#perform_download(subdir, 'repodata_from_packages.json')for pack_name, pack_info in repos_info['packages'].items():if subdir == 'noarch':perform_download(subdir, pack_name)else:if 'py' not in pack_info['build'] or self.py_version in pack_info['build'] or 'py_0' in pack_info['build']:perform_download(subdir, pack_name)def _get_repo_info(self, repo, suffix):repo_data_path = os.path.join(repo, suffix, 'repodata.json')        req = urllib.request.Request(repo_data_path)with urllib.request.urlopen(req) as response:data = json.load(response)return datadef start_download(self):workers = []for repo in self.main_repos:if 'free' in repo:subdir = 'free'else:subdir = 'main'self._create_subdir(subdir)for suffix in self.all_suffixes:repos_info = self._get_repo_info(repo, suffix)self._download_libs(repos_info, repo, subdir)def parse_options():parser = optparse.OptionParser()parser.add_option('-c','--channel-dir', action='store',default='', dest='channel_dir',help='the setup directory of the conda channel')parser.add_option('-w','--windows-download', action='store',default='false', dest='win_down',help='true or false to download windows libraries')parser.add_option('-l','--linux-download', action='store',default='false', dest='linux_down',help='true or false to download linux libraries')parser.add_option('-m','--mac-download', action='store',default='false', dest='mac_down',help='true or false to download mac libraries')parser.add_option('-p','--python-version', action='store',default='3.3', dest='py_version',help='The python version for which to seek libraries 3.x format')opts, args = parser.parse_args()return opts, argsdef start():opts, args = parse_options()bool_str = ('true', 'false')py_versions = ('3.0', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8')if not opts.channel_dir.strip():print('Missing --channel-dir argument. Run --help for details')raise SystemExit(1)if opts.win_down.lower() not in bool_str:print('Incorrect --windows-download argument. Run --help for details')raise SystemExit(1)if opts.linux_down.lower() not in bool_str:print('Incorrect --linux-download argument. Run --help for details')raise SystemExit(1)if opts.mac_down.lower() not in bool_str:print('Incorrect --mac-download argument. Run --help for details')raise SystemExit(1)if opts.py_version.strip() not in py_versions:print('Incorrect --python-version argument. Run --help for details')raise SystemExit(1)cr_obj = CondaRepo(opts.py_version.strip(),opts.win_down.lower(),opts.linux_down.lower(),opts.mac_down.lower())cr_obj.output_path = opts.channel_dir.strip()cr_obj.start_download()start()

通道建立后,任何用户都可以访问该通道并安装所需的库。用户需要有一个本地miniconda安装。Conda需要设置为查找创建的通道,而不是在互联网上的远程存储库。要做到这一点,位于用户主目录下的.condarc必须看起来像这样:

channels:- http://path/to_my/conda_channel/main/- http://path/to_my/conda_channel/free/

此时,用户可以按照上面的说明创建一个python 3.8环境并安装库。

安装Matplotlib

Matplotlib是一个2D绘图库。

为了安装Matplotlib,在前面创建的conda环境中执行:

conda install matplotlib

下面的例子展示了如何在ANSA或META中执行Matplolib代码:

import ansa
import sysCONDA_ENV = '/home/my_name/miniconda3/envs/python38/lib/python3.8/site-packages'
sys.path.append(CONDA_ENV)import matplotlib
from matplotlib import pyplot as pltdef my_plot():x_vals = [0.2, 0.7, 1.2, 2.7, 3.5]y_vals = [1.2, 1.8, 2.4, 3.9, 4.5]plt.suptitle('My Graph')ax = plt.subplot('111')ax.plot(x_vals, y_vals)plt.show()

其他库

可以安装和导入其他库到ANSA或META中。例如:Pandas, SymPy等。

注:
在Conda环境中搜索安装新库时,应特别注意确保该库与Python 3.8兼容。


本教程来自为ANSA21帮助文档


扫描下方二维码关注我的微信公众号 - CAE软件二次开发Lab,精彩内容!

CAE软件二次开发Lab


ANSA二次开发 - 在ANSA中导入外部Python库相关推荐

  1. ANSA二次开发 - 安装外部python库(以xlrd为例)

      Python由于其易用性和良好的可扩展性以及丰富的第三方库,而被越来越多的CAX软件作为二次开发的脚本语言.   ANSA前处理软件得二次开发语言就是Python语言,但其与常规Python发行版 ...

  2. ANSA二次开发 - 抽中面的两种方法

    文章目录 (一) 使用Skin命令抽中面. (二) 使用Casting抽中面.   ANSA是一款非常强大和快捷的前处理软件,用过的人都知道,ANSA在几何清理.模型简化功能方面尤为突出.不仅如此,A ...

  3. ANSA二次开发 - 加密py文件为pyd格式

    文章目录 加密过程 CompileScript加密函数 函数名称 函数语法 功能描述 函数参数 返回类型 返回值 示例   有时候我们希望保护所开发的ANSA二次开发python代码,需要对明文代码进 ...

  4. ANSA二次开发——初识Entity

    在之前的四篇文章中 ANSA二次开发--BCGUI简介(1) ANSA二次开发--BCGUI简介(2) ANSA二次开发--BCGUI简介(3) ANSA二次开发--BCGUI简介(4) 我们已经对A ...

  5. ANSA二次开发 - PID筛选案例介绍

      今天我们来介绍一个二次开发案例-ANSA中零件PID的筛选与操作.   首先先看一下需求:   如上图所示.有一堆零件,软件已经自动区分出PID.它们的PID名字都以"part" ...

  6. python仿真搭建_仿真秀学院|从零开始学ANSA二次开发:如何利用Designer搭建窗口,含安装步骤...

    原标题:仿真秀学院|从零开始学ANSA二次开发:如何利用Designer搭建窗口,含安装步骤 作者 | 团长 仿真秀专栏作者 首发 |仿真秀App 导读:大家好,我是团长,是一名CAEer,还是一名C ...

  7. ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

    文章目录 与PyCharm IDE集成 简介 ANSA和META自动补全 安装说明 在PyCharm中工作 与PyCharm IDE集成 简介   PyCharm 是一个集成开发环境(IDE).它是专 ...

  8. ANSA二次开发——BCGUI简介(3)

    ANSA二次开发--BCGUI简介(3) 上篇文章ANSA二次开发--BCGUI简介(2)主要介绍了在创建GUI界面中常见的组件创建函数,那如何在窗口中将各种不同的组件排列出我们想要的布局呢?下面就介 ...

  9. NX二次开发-获得制图中对象的坐标点UF_DRF_ask_origin

    NX二次开发-获得制图中对象的坐标点UF_DRF_ask_origin #include <uf.h> #include <uf_ui.h> #include <uf_d ...

最新文章

  1. 2022-2028年中国商业综合体行业市场前瞻与投资规划分析报告
  2. python控制结构实训_《python 从入门到精通》§5 控制结构
  3. batchelor包去除单细胞RNA-seq数据批次效应
  4. max格式转obj小工具_Python写图片格式批量处理工具!你还一张一张转格式吗?
  5. Windows WorkFlow Foundation学习资源
  6. java tessdata训练_Tesseract For Java为可执行jar设置Tessdata_Prefix
  7. [渝粤教育] 西南科技大学 质量与可靠性管理 在线考试复习资料
  8. Docker-Oracle和物理机Oracle数据库性能测试
  9. 关于Bootstrap的理解
  10. 网络管理与维护作业4
  11. WPF下通过附加属性实现单实例启动
  12. 转:高级PHP应用程序漏洞审核技术
  13. 算法竞赛入门经典——1
  14. mysql触发器更新前触发_mysql触发器实例:更新前触发
  15. jQuery获取表单数据
  16. html5置顶标签css样式,html5 header标签 html header css布局教程 /header
  17. 腾讯+android+hotfix,发布到安卓平台报这个错误,xLua exception : xlua.access, no field __Hotfix0_Update...
  18. python调用rf关键字_RobotFramework之关键字
  19. 递归实现部门树形数据结构
  20. 安卓开发 高德地图定位的封装 十分钟上手

热门文章

  1. notepad++ 行尾行首添加字符串-win32-管道技术-共享内存技术-父子进程数据交换-在CSDN中如何添加目录-Python-字体转文字
  2. Mycat源码修改分表规则的CRUD操作测试与改进
  3. 图片(txt等)实现默认下载而不是浏览器默认打开(Java版)
  4. 激光SLAM闭环方案总结
  5. html字体加粗字号为2,【css】文字基本样式(颜色、倾斜、加粗、字号、行高、字体)...
  6. 调用百度API,获取身份证上的信息
  7. python参数寻优_Libsvm网格参数寻优教程
  8. 操作系统-linux-内存-缓存
  9. 【随笔】再见IE浏览器,一个时代的印记从此消失
  10. 炫舞滑板机器人_当音速配上滑板,微飞爱场地助力腾讯炫舞时代