Home > other >  The import can, from the import doesn't (work well)
The import can, from the import doesn't (work well)

Time:09-24

Call python in c + +, python program in import __future__ can become the from __future__ import absolute_import, PyImport_ImportModule returns empty, don't know why, for the great spirit guide

CodePudding user response:

From __future__ import absolute_import
You add a point in front of the __future__ & lt; . & gt; , said the current path, this kind of question I stepped over a considerable amount of pit, or the path is added, are used. The way

CodePudding user response:

A little also not line, because there is no current path __future__, tried to use absolute paths and the relative path to __future__ directory, all not line, don't know why

CodePudding user response:

refer to the second floor xiajianfeng147 response:
a little also not line, because there is no current path __future__, tried to use absolute paths and relative positioning to directory __future__, no, don't know why

Absolute path using the method is: from the packagename1. Packagename2. Packagename3. __future__ import absolute_import this way
Relative path is used from __future__ import absolute_import this way, you can put the package in the current file path try

CodePudding user response:

reference MuSa xiaoxiang reply: 3/f
Quote: refer to the second floor xiajianfeng147 response:

A little also not line, because there is no current path __future__, tried to use absolute paths and the relative path to __future__ directory, no, don't know why

Absolute path using the method is: from the packagename1. Packagename2. Packagename3. __future__ import absolute_import this way
Relative path is used from __future__ import absolute_import this way, you can put the package in the current file path try


Changed all sorts of paths are not ah, the code is like this:

Test. Py

#! The/usr/bin/python
# - * - coding: utf-8 - * -
The import sys

Sys. Path. Append ('../Rasa_NLU_Chi - master/rasa_nlu/')
Sys. Path. Append ('../Rasa_NLU_Chi - master/')

# # # # import __future__ it can run
The from __future__ import absolute_import # # # # for this PyImport_ImportModule returns NULL

The main. CPP
# include & lt; Iostream>
# include & lt; Python. H>
using namespace std;
Int main ()
{
Py_Initialize ();

if (! Py_IsInitialized ())
{
return -1;
}

PyRun_SimpleString (" import sys ");
PyRun_SimpleString (" sys. Path. Append ('/') ");
PyRun_SimpleString (" sys. Path. Append ('../') ");
//PyRun_SimpleString (" print (sys. Path) ");
The PyObject * pModule=NULL;//declare variables
The PyObject * pFunc=NULL;//declare variables
The PyObject * pParm=NULL;
PModule=PyImport_ImportModule (" test ");
If (pModule==NULL)
{
cout <"Not found" & lt; }
return 0;
}

CodePudding user response:

Sys. Path usage is not wrong, do you have in this test. The py file directory at the next higher level respectively Rasa_NLU_Chi - master, rasa_nlu both directories, this is put inside the file you want to call

CodePudding user response:

__future__ this file in sys/usr/lib/python3.5 directory, the import can according to the truth, from the import

CodePudding user response:

The corresponding python program in pycharm run there is no problem
  • Related