golang调⽤python3,并使⽤python模块中的⽅法
python代码转换因项⽬需要,很多代码和python模块是go语⾔没有的,虽然有个项⽬是转化python代码到golang代码,但是还没开始⽤,关键是python 引⽤的模块如此之多,不可能都去转换对吧。
py3安装⽅法
*************************************
1.先安装chocolatey
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
Getting latest version of the Chocolatey package for download.
Not using proxy.
Not using proxy.
Extracting C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to
C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocoInstall
Installing Chocolatey on the local machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
(i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.
Creating Chocolatey folders if they do not already exist.
WARNING: You can safely ignore errors related to missing log files when
upgrading from a version of Chocolatey less than 0.9.9.
'Batch file could not be found' is also safe to ignore.
'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it.
警告: Not setting tab completion: Profile file does not exist at
'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey () is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
Ensuring Chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
PS C:\Users\Administrator> choco -v
0.11.3
PS C:\Users\Administrator> choco upgrade chocolatey
Chocolatey v0.11.3
Upgrading the following packages:
chocolatey
By upgrading, you accept licenses for the packages.
chocolatey v0.11.3 is the latest version available based on your source(s).
Chocolatey upgraded 0/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Did you know the proceeds of Pro (and some proceeds from other
licensed editions) go into bettering the community infrastructure?
Your support ensures an active community, keeps Chocolatey tip-top,
plus it nets you some awesome features!
PS C:\Users\Administrator>
2.下载github/christian-korneck/snack
3.执⾏build.bat⽂件,⾃动安装python和设置pkg-config
4.修改go.mod中的go 1.17版本为你当前安装的版本号,⽐如go 1.16
5.然后go mod tidy更新mod⽂件
6.进⼊⽂件夹内执⾏go 编译⽣成exe⽂件,将C:\dist\python3
7.dll拷贝到exe⽂件所在⽂件夹内才能运⾏
7.刚才那个bat⽂件没有安装pip,所以要在PowerShell⾥安装pip
$ curl bootstrap.pypa.io/get-pip.py -o get-pip.py # 下载安装脚本
注意:⽤哪个版本的 Python 运⾏安装脚本,pip 就被关联到哪个版本,如果是 Python3 则执⾏以下命令:
Win下:python get-pip.py
PS C:\Users\Administrator> pip -V
pip 21.3.1 from c:\py\lib\site-packages\pip (python 3.7)
8.开始安装依赖包:设置国内代理,否则超级慢
Collecting ansys-mapdl-reader
Using cached ansys_mapdl_reader-0.51.7-cp37-cp37m-win_amd64.whl (1.1 MB)
Collecting matplotlib>=3.0.0
Using cached matplotlib-3.5.1-cp37-cp37m-win_amd64.whl (7.2 MB)
Collecting tqdm>=4.45.0
Using cached tqdm-4.62.3-py2.py3-none-any.whl (76 kB)
Collecting appdirs>=1.4.0
Using cached appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
<-py3例⼦
调⽤python模块中的⽅法
⽐如,⼀个python的代码如下
# plot.py
import os,sys
import ader as aaa
#import pyvista as pv
# from ansys.mapdl import reader as pymapdl_reader
# from ader import examples
# import ansys.mapdl
# import ader
#import pyansys
#dir_path = os.path.dirname(alpath(__file__)) #⽬录路径
#print(dir_path)
#rstfile = os.path.join(dir_path, '1641543541831483300_0.rst') #本地rst⽂件
>###⽹上例⼦>>>>>>>>>>>>####
# def read_ansys():
# dir_path = os.path.dirname(alpath(__file__)) #⽬录路径
# print(dir_path)
# rstfile = os.path.join(dir_path, '2.rst') #本地rst⽂件
# rstfile = examples.rstfile #⽹上例⼦
# result = ad_binary('2.rst') #新版读⽂件
# freqs = result.time_values
# print(freqs)
# nnum, disp = dal_solution(0)
# print(disp)
# cpos = result.plot_nodal_solution(0) #显⽰到窗⼝
# vtkfile = os.path.join('', 'hexrst1.vtk')
# result.save_as_vtk('hexrst1.vtk')
>>>>>>>>>>>>>>>####
# if __name__ == '__main__':
# it(read_ansys())
在python中,上述代码⽤aaa包中的read_binary⽅法来读取⼀个⽂件夹⾥的⽂件对吧。到go⾥怎么做呢?
package main
import (
"fmt"
"github/DataDog/go-python3"
"log"
"os"
)
func init() {
// 1. 初始化python环境
python3.Py_Initialize()
if !python3.Py_IsInitialized() {
fmt.Println("Error initializing the python interpreter")
os.Exit(1)
}
}
func main() {
// 2. 设置本地python import 的路径
// p := "C:/py/Lib/site-packages"
// InsertBeforeSysPath(p)
// 3. 导⼊hello模块
plot := ImportModule("D:/snack/ansys", "plot") //导⼊python代码plot.py
qin := plot.GetAttrString("aaa") //获取函数-plot.py⾥的导⼊包import ader as aaa
log.Print(qin)
sep := python3.PyUnicode_FromString("2.rst") //必须是ansys14+⽣成的rst⽂件!!
res2 := qin.CallMethodArgs("read_binary", sep)
log.Print(res2)
sep0 := python3.PyLong_FromGoInt(0)
cpos := res2.CallMethodArgs("plot_nodal_solution", sep0) //显⽰到窗⼝
log.Print(cpos)
sep2 := python3.PyUnicode_FromString("hexrst3.vtk")
res3 := res2.CallMethodArgs("save_as_vtk", sep2)
log.Print(res3)
注意到没有,是先导⼊这个代码⽂件plot.py,然后再获取py代码⾥引⼊的模块aaa,开始使⽤aaa⾥的read_binary⽅法,⽤callmethodargs,⽅法名称写在括号⾥,参数写在⽅法名称后⾯。代码有点乱,看仔细点就清楚了。好了,就是下⾯这个效果⽽已。
这个3d图可是可以转动的哦,vtk真是厉害。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论