Lumerical Python API配置-一步到位

Lumerical Python API配置-一步到位

1、下载安装好Lumerical

2、下载官网最新版本的Python或者anaconda

3、一步到位,将*\Lumerical\v231\python-3.9.9-embed-amd64\文件下的python36.pth文件复制粘贴到安装好的python或anaconda的*\python目录下

4、修改*\python目录下的python36.pth内容,如下

your install path\Lumerical\v231\api\python

your install path\Python\dlls

是不是很简单!!!先给出测试效果!!!代码如下,python的版本是3.11

图中红色部分表示python版本3.11.3.64-bit,被水印挡住了

 测试结果

测试结果,软件正常打开

 也能够正常进行代码智能补齐

好了开始我的思考了,下面是我如何找到的,有空的大家去看看

Lumerical各仿真软件 (如FDTD, DEVICE, INTERCONNECT) 与Python编程语言的交互,依靠仿真软件预留的接口Python API实现,这种接口具体而言就是一个叫做lumapi的Python库。导入lumapi库并初始化会话即可实现仿真软件与Python的交互,该过程需要GUI lisence。Lumerical的解释器附带了一个基本的Python 3发行版供用户使用[1],可以直接从软件安装路径中的\Lumerical\v231\python-3.9.9-embed-amd64\打开python.exe并导入lumapi库:

import lumapi

[2]直接使用Lumerical自带的解释器可能不能满足使用者的需求,比如我可能需要调用第三方包,由于根目录\Lumerical\v231\python-3.9.9-embed-amd64\ 下Scripts文件夹没有pip.exe或者pip3.exe执行文件,导致无法安装第三方库,出现报错,见[2]。转[3]

由于该环境不集成pip包,所以需要手动安装。
首先下载get-pip.py脚本,可以参考官方教程:https://pip.pypa.io/en/stable/installation
打开Windows命令行,cd到该python环境根目录,执行python get-pip.py,会自动下载并安装pip包。
pip安装完成后,会自动在该环境根目录创建Lib\site-packagesScripts文件夹,分别包含通过pip安装的第三方包和pip工具本身。

但这种方式只能在原来旧版本上安装第三方库文件,需要注意第三方库文件与Lumerical软件自带的文件相冲突的问题,例如对numpy包进行更新,然后在下载pandas包,可能会出现多个相同文件,导致一堆BUG,本人就出现这种问题,最后问题太多,直接卸载重装。

最后本人冷静下来,思考自己的目的:利用现成的最新python版本对Lumerical的API包进行调用。参考官网 [4][5],如下。我的python版本是3.11,而lumeirical的版本是3.9。

Many users would like to integrate lumapi into their own Python environment. This is as simple as importing the lumapi module; however, by default python will not know where to find it. This can be addressed by modifying the search path. Please refer to the Python foundations guide on installing modules. Essentially we allow Python to find the lumapi module, by including its parent directory in the list that import class will search. Usually, a one-time update, see explicit path import for the OS specific directories. If you are not ready or to update the Python path then either of the following methods should work

Adding the Python Before Importing

To temporarily add the lumapi directory to your path, you can use the sys.path.append() method. This is the case if you have not yet added lumapi to your search path, and it is also useful when adding directories of other helpful lsf, fsp, py files. The following code adds the lumapi folder and current file directory.

import sys, os
sys.path.append("C:\\Program Files\\Lumerical\\v231\\api\\python\\") #Default windows lumapi path
sys.path.append("/opt/lumerical/v231/api/python/lumapi.py") #Default linux lumapi path
sys.path.append(os.path.dirname(__file__)) #Current directory

Explicit Import

If you need to specify the path to your lumapi distribution then the importlib.util() is the best method for pointing to the lumapi module. This may be the case if you are working on a different branch, or comparing lumapi versions. 

import importlib.util
#The default paths for windows and linux
spec_win = importlib.util.spec_from_file_location('lumapi', 'C:\\Program Files\\Lumerical\\v231\\api\\python\\lumapi.py')
spec_lin = importlib.util.spec_from_file_location('lumapi', "/opt/lumerical/v231/api/python/lumapi.py")
#Functions that perform the actual loading
lumapi = importlib.util.module_from_spec(spec_win) #windows
lumapi = importlib.util.module_from_spec(spec_lin) #linux
spec.loader.exec_module(lumapi)

 但这种操作依然可能会出现这样的问题

FileNotFoundError:

Could not find module 'interopapi.dll' (or one of its dependencies). Try using the full path with constructor syntax.

 这是因为python版本问题,尤其是老版本3.6,因为从3.7版本之后不再支持3.6之前的一些文件。

本人不清楚在最新python上按照这样的操作是否可以不出现这样的问题,没有进行测试,但如果每次都在文件中添加路径,则不会出现代码智能补齐的效果

 因此在阅读完这篇文章https://www.jianshu.com/p/8bd34d13415e之后,突发灵感,因为目录\Lumerical\v231\python-3.9.9-embed-amd64\中Lumeical的python版本是嵌入式版本,与普通python安装后不同,嵌入式版本会多一个文件python36.pth,如图所示。

 python36.pth的内容如下,也就是说默认情况下该python环境只会从
D:\CommonSoftware\Lumerical\v231\api\python文件夹和D:\CommonSoftware\Lumerical\v231\python-3.9.9-embed-amd64\dlls文件夹搜索可导入的包。其中.\api\python文件夹里面的内容就有lumapi.py和lumopt.py文件,而dlls文件夹里面的文件与其自带的库文件有关。


D:\CommonSoftware\Lumerical\v231\api\python

D:\CommonSoftware\Lumerical\v231\python-3.9.9-embed-amd64\dlls

因此我们可以将python36.pth文件复制粘贴到自己安装的python3.11版本的目录D:\CommonSoftware\Python下。同时我们还可以根据自己的需要,手动修改该文件来配置包导入其他搜索路径,来满足自己的个性化需求。如下图所示,文件名可以修改为python311.pth,也可以不修改,本人进行了修改。第一条保持不变,继续引用Lumerical的API包,第二条是引用Python3.11自带的库文件。由于python3.11自带pip命令,因此我们可以直接通过pip命令来安装更新库文件,到这一步了想必进入了大家非常熟悉的步骤了。

D:\CommonSoftware\Lumerical\v231\api\python

D:\CommonSoftware\Python\dlls

题外:是否可以通过anaconda包来控制Lumerical API呢?这个留给大家自己去尝试了,有问题希望大家留言

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值