1

Using Python 2.7, OS: Ubuntu 16.04 64 bit.

I am writing one sample application using Python C API. I have created one virtual environment and installed dependent package in virtualenv to run this application. Below is my code.

#include <iostream>
#include <Python.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]){    
  Py_OptimizeFlag=1;
  //Py_SetPythonHome((char *)"/home/user/workspace/venv_2_7/bin/python");
  Py_SetProgramName((char *)"python_test");
  Py_Initialize();

  PySys_SetPath((char *)"/home/user/workspace/venv_2_7/lib;/home/user/workspace/venv_2_7/lib/python2.7/site-packages;");

  PySys_SetArgv(argc, argv);

  PyObject* PyFileObject = PyFile_FromString((char *)"/home/user/Projects/python_test/test.py", (char *)"r");
  int ret = PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), (char *)"/home/user/Projects/python_test/test.py", 1);

  if (ret != 0) {
    printf ("Failed to launch the application.\n");
    return 0;
  }

  Py_Finalize();
  return 0;
}

As I run this file, I am getting this error:

Traceback (most recent call last):
  File "/home/user/Projects/python_test/test.py", line 23, in <module>
    ....
  File "/home/user/Projects/python_test/sample/__init__.py", line 12,     in <module>
    import logging
ImportError: No module named logging

The message clearly says that system and other packages required for application are not found when we run the application. Even though I set the path, it is not found.

  • How to tell application where to find the all the required system and other packages?
  • How to set through API so that we did not get error?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Neel
  • 451
  • 1
  • 9
  • 23

1 Answers1

0

I think that the first item in your path should include the python2.7 folder, as that will be where the standard library files should be, not in lib.

/home/user/workspace/venv_2_7/lib/python2.7

But you should double check that they are there. If your virtualenv tool is older then it may have a different default behavior in this area.

RobinDunn
  • 6,116
  • 1
  • 15
  • 14