因项目需要,很多代码和python模块是go语言没有的,虽然有个项目是转化python代码到golang代码,但是还没开始用,关键是python引用的模块如此之多,不可能都去转换对吧。

py3安装方法

*************************************

1.先安装chocolatey

Windows PowerShell

版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6 PS C:UsersAdministrator> Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)

Getting latest version of the Chocolatey package for download.

Not using proxy.

Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/0.11.3.

Downloading https://community.chocolatey.org/api/v2/package/chocolatey/0.11.3 to C:UsersAdministratorAppDataLocalTempchocolateychocoInstallchocolatey.zip

Not using proxy.

Extracting C:UsersAdministratorAppDataLocalTempchocolateychocoInstallchocolatey.zip to C:UsersAdministratorAppDataLocalTempchocolateychocoInstall

Installing Chocolatey on the local machine

Creating ChocolateyInstall as an environment variable (targeting 'Machine')

Setting ChocolateyInstall to 'C:ProgramDatachocolatey'

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:ProgramDatachocolateylib'

(i.e. C:ProgramDatachocolateylibyourPackageName).

A shim file for the command line goes to 'C:ProgramDatachocolateybin'

and points to an executable in 'C:ProgramDatachocolateylibyourPackageName'.

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:ProgramDatachocolateybin in it. Adding...

警告: Not setting tab completion: Profile file does not exist at

'C:UsersAdministratorDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1'.

Chocolatey (choco.exe) 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:UsersAdministrator> choco -v

0.11.3

PS C:UsersAdministrator> 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:ProgramDatachocolateylogschocolatey.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!

https://chocolatey.org/compare

PS C:UsersAdministrator>

2.下载https://github.com/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 run main.go编译生成exe文件,将C:distpython37.dll拷贝到exe文件所在文件夹内才能运行

7.刚才那个bat文件没有安装pip,所以要在PowerShell里安装pip

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py # 下载安装脚本

$ sudo python get-pip.py # 运行安装脚本

注意:用哪个版本的 Python 运行安装脚本,pip 就被关联到哪个版本,如果是 Python3 则执行以下命令:

$ sudo python3 get-pip.py # 运行安装脚本。

Win下:python get-pip.py

PS C:UsersAdministrator> pip -V

pip 21.3.1 from c:pylibsite-packagespip (python 3.7)

8.开始安装依赖包:设置国内代理,否则超级慢

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansys-mapdl-reader

PS C:UsersAdministrator> pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansys-mapdl-reader

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)

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyvista

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansys-mapdl-core

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansys-dpf-post

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansys-dpf-core

9.go-py3例子

调用python模块中的方法

比如,一个python的代码如下

在python中,上述代码用aaa包中的read_binary方法来读取一个文件夹里的文件对吧。到go里怎么做呢?

注意到没有,是先导入这个代码文件plot.py,然后再获取py代码里引入的模块aaa,开始使用aaa里的read_binary方法,用callmethodargs,方法名称写在括号里,参数写在方法名称后面。代码有点乱,看仔细点就清楚了。好了,就是下面这个效果而已。

这个3d图可是可以转动的哦,vtk真是厉害。

有时候编译go又出现#include **.h 找不到错误,则运行bat文件,注释掉安装python部分。

注释掉(rem)build.bat中安装python部分,如下

然后点击运行build.bat即可修复。