Skip to content

Commit 9190142

Browse files
committed
Support C++ import python on windows for paddle
1 parent 0438b60 commit 9190142

5 files changed

Lines changed: 86 additions & 6 deletions

File tree

python/paddle/dataset/image.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@
3939
if six.PY3:
4040
import subprocess
4141
import sys
42+
if sys.platform == 'win32':
43+
interpreter = sys.exec_prefix + "\\" + "python"
44+
else:
45+
interpreter = sys.executable
4246
import_cv2_proc = subprocess.Popen(
43-
[sys.executable, "-c", "import cv2"],
47+
[interpreter, "-c", "import cv2"],
4448
stdout=subprocess.PIPE,
4549
stderr=subprocess.PIPE)
46-
out, err = import_cv2_proc.communicate()
4750
retcode = import_cv2_proc.poll()
4851
if retcode != 0:
4952
cv2 = None

python/paddle/fluid/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,13 @@ def __bootstrap__():
247247
]
248248

249249
core.init_gflags(["--tryfromenv=" + ",".join(read_env_flags)])
250-
core.init_glog(sys.argv[0])
250+
# Note(zhouwei25): sys may not have argv in some cases,
251+
# Such as: use Python/C API to call Python from C++
252+
try:
253+
core.init_glog(sys.argv[0])
254+
except Exception:
255+
sys.argv = [""]
256+
core.init_glog(sys.argv[0])
251257
# don't init_p2p when in unittest to save time.
252258
core.init_devices()
253259

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ if (WITH_XPU_BKCL)
670670
py_test(test_collective_allreduce_api_xpu SRCS "test_collective_allreduce_api.py")
671671
endif()
672672

673+
if(WIN32)
674+
cc_test(cc_imp_py_test SRCS cc_imp_py_test.cc DEPS python)
675+
endif()
676+
673677
if (WITH_ASCEND_CL)
674678
add_subdirectory(npu)
675679
endif()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
#include <iostream>
17+
#include "Python.h"
18+
19+
TEST(CC, IMPORT_PY) {
20+
// Initialize python environment
21+
Py_Initialize();
22+
ASSERT_TRUE(Py_IsInitialized());
23+
24+
// 1. C/C++ Run Python simple string
25+
std::cout << PyRun_SimpleString("import paddle");
26+
ASSERT_FALSE(PyRun_SimpleString("print(paddle.to_tensor(1))"));
27+
28+
// 2. C/C++ Run Python funciton
29+
PyRun_SimpleString("import sys");
30+
PyRun_SimpleString("import os");
31+
PyRun_SimpleString("sys.path.append(os.getcwd())");
32+
PyObject* pModule = PyImport_ImportModule("test_install_check");
33+
ASSERT_TRUE(pModule != NULL);
34+
35+
PyObject* pTestInt = PyObject_GetAttrString(pModule, "TestInt");
36+
ASSERT_TRUE(pTestInt != NULL);
37+
PyObject* pArg1 = PyObject_CallObject(pTestInt, NULL);
38+
ASSERT_TRUE(pArg1 != NULL);
39+
int result;
40+
ASSERT_TRUE(PyArg_Parse(pArg1, "i", &result));
41+
ASSERT_EQ(result, 100);
42+
43+
PyObject* pTestString = PyObject_GetAttrString(pModule, "TestString");
44+
ASSERT_TRUE(pTestString != NULL);
45+
PyObject* pArg2 = PyObject_CallObject(pTestString, NULL);
46+
ASSERT_TRUE(pArg2 != NULL);
47+
char* cwd;
48+
ASSERT_TRUE(PyArg_Parse(pArg2, "s", &cwd));
49+
50+
// 3. C/C++ Run Python file
51+
std::string file_name(cwd);
52+
file_name.append("/test_install_check.py");
53+
FILE* fp = _Py_fopen(file_name.c_str(), "r+");
54+
ASSERT_TRUE(fp != NULL);
55+
ASSERT_FALSE(PyRun_SimpleFile(fp, file_name.c_str()));
56+
57+
// Uninitialize python environment
58+
Py_Finalize();
59+
ASSERT_FALSE(Py_IsInitialized());
60+
}

python/paddle/fluid/tests/unittests/test_install_check.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from __future__ import print_function
1615
import unittest
1716
import paddle
18-
import paddle.fluid as fluid
17+
import os
1918

2019

2120
class TestInstallCheck(unittest.TestCase):
2221
def test_paddle_fluid(self):
23-
fluid.install_check.run_check()
22+
paddle.fluid.install_check.run_check()
2423

2524
def test_paddle_utils(self):
2625
paddle.utils.run_check()
2726

2827

28+
def TestInt():
29+
return 100
30+
31+
32+
def TestString():
33+
return os.getcwd()
34+
35+
2936
if __name__ == '__main__':
3037
unittest.main()

0 commit comments

Comments
 (0)