[英]How to call a python file, that needs to import packages?
I'm following a tutorial to call python code from a C++ program from the python docs . 我正在按照一个教程从python docs的C ++程序调用python代码。
Everything works just fine when trying to call the multiply
example. 尝试调用
multiply
示例时,一切正常。 Now if I add a line to the python source code importing a library, lets say openpyxl
, 现在,如果我在导入库的python源代码中添加一行,则可以说
openpyxl
,
from openpyxl import load_workbook
I receive an error from python 我收到python错误
ImportError: No module named openpyxl
I thought if I import a system library , I wouldn't have any problems, but I also get an error if I try to import datetime
. 我以为如果导入系统库 ,不会有任何问题,但是如果尝试导入
datetime
,也会出现错误。
I don't have any error if I import the file from the python console. 如果我从python控制台导入文件,则没有任何错误。 The
openpyxl
library is installed in my system. openpyxl
库已安装在我的系统中。
So my question is: how to import python source code that needs to import packages? 所以我的问题是:如何导入需要导入包的python源代码?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry. 编辑:好的,我忘了提些什么,对你们我还没有完全诚实,对不起。
Trying to run the example I run into a problem: I couldn't make python found my multiply.py
file, and the line PyImport_Import
always return null
. 试图运行我碰到一个问题的例子:我不能让蟒蛇发现我
multiply.py
文件,行PyImport_Import
总是返回null
。
My solution was to add the path in which I knew my python source was by using PySys_SetPath
. 我的解决方案是使用
PySys_SetPath
添加我知道我的python源的路径。 The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH
. 问题是我只是意识到该函数不会追加新目录,而只是覆盖
PYTHONPATH
。 So now python can find multiply.py
, but absolutly anything else. 因此,现在python可以找到
multiply.py
,但是绝对没有其他任何东西。
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program? 当然,我已经删除了该行,但是现在我还有另一个问题,如果文件只是在C ++编译程序的同一目录中,为什么python找不到我的源代码?
The I realized that my sys.path
from my python console was a little different from the path showed in my embedded python : the first one had at the beginning of the list an empty string ''
. 我意识到我的python控制台的
sys.path
与嵌入式python中显示的路径有点不同 :第一个在列表的开头有一个空字符串''
。 I'm not a python expert, but when I add that line to my path I could import the multiply.py
so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-. 我不是python专家,但是当我将该行添加到路径中时,我可以导入
multiply.py
因此这似乎就是无法导入相对于我的可执行文件相对的模块的原因空路径-但仍然不知道这意味着什么。
I have to thank to @paul-evans who give me the idea of adding the path to find my files. 我要感谢@ paul-evans,他们给了我添加查找文件路径的想法。
This is what PYTHONPATH
is for. 这就是
PYTHONPATH
目的。 You can set it as an environment variable containing a list module directories, or in the code itself something like: 您可以将其设置为包含列表模块目录的环境变量,或者在代码本身中将其设置为:
import sys
sys.path.append("path/to/openpyxl/module")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.