
PyObject *sysPath = PySys_Getobject("path");PyObject *path = PyBytes_FromString(".");int result = PyList_Insert(sysPath,path); 这工作正常(没有错误),但是当我尝试运行模块时Python不高兴:
PyObject *pModule = Pyimport_importModule("python_demo_x"); Python报告的错误是:
Could not load module python_demo_xTraceback (most recent call last): file "<froZen importlib._bootstrap>",line 961,in _find_and_load file "<froZen importlib._bootstrap>",line 946,in _find_and_load_unlocked file "<froZen importlib._bootstrap>",line 885,in _find_spec file "<froZen importlib._bootstrap_external>",line 1157,in find_spec file "<froZen importlib._bootstrap_external>",line 1129,in _get_spec file "<froZen importlib._bootstrap_external>",line 1245,line 1302,in _fill_cacheTypeError: a bytes-like object is required,not 'str'
通过反复试验,我发现用双引号包装路径解决了这个问题:
PyObject *path = PyBytes_FromString("\".\""); 我找不到任何表明包装路径是必需的文档.这是必需的,还是有其他错误?
解决方法 Python 3改变了字符串的表示方式.我在使用Popen从外部执行其他应用程序和代码时遇到过这种情况.
尝试将所有字符串从“some-string”更改为b“some-string”,将其编码为字节字符串.
如果需要字符串返回值,则可能需要将生成的字节对象解码为字符串.
你会这样做:.decode(‘utf-8’)
从Python 3.6文档:
“在Python 3.x中,那些隐式转换已经消失 – 8位二进制数据和Unicode文本之间的转换必须是显式的,字节和字符串对象总是比较不相等.”
更多信息请访问:https://docs.python.org/3/library/stdtypes.html
总结以上是内存溢出为你收集整理的python-3.x – Python 3.6,嵌入式C,添加模块路径,TypeError:需要类似字节的对象,而不是’str’全部内容,希望文章能够帮你解决python-3.x – Python 3.6,嵌入式C,添加模块路径,TypeError:需要类似字节的对象,而不是’str’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)