diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index fa2c1b97587d25f20b321931571c748145ca2843..c6c84f8b9f0e56165ea07a75c4cf22c6dad3fcd4 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -53,6 +53,11 @@ std::once_flag glog_warning_once_flag; void InitGflags(std::vector argv) { std::call_once(gflags_init_flag, [&]() { FLAGS_logtostderr = true; + // NOTE(zhiqiu): dummy is needed, since the function + // ParseNewCommandLineFlags in gflags.cc starts processing + // commandline strings from idx 1. + // The reason is, it assumes that the first one (idx 0) is + // the filename of executable file. argv.insert(argv.begin(), "dummy"); int argc = argv.size(); char **arr = new char *[argv.size()]; @@ -62,8 +67,10 @@ void InitGflags(std::vector argv) { line += argv[i]; line += ' '; } + VLOG(1) << "Before Parse: argc is " << argc + << ", Init commandline: " << line; google::ParseCommandLineFlags(&argc, &arr, true); - VLOG(1) << "Init commandline: " << line; + VLOG(1) << "After Parse: argc is " << argc; }); } diff --git a/python/paddle/fluid/__init__.py b/python/paddle/fluid/__init__.py index 04a70c5282c2ada03f1dacc445d3e9abf829e41d..16e411fa243d4974e9caba8f1724d9e74cb71887 100644 --- a/python/paddle/fluid/__init__.py +++ b/python/paddle/fluid/__init__.py @@ -214,8 +214,7 @@ def __bootstrap__(): 'cudnn_batchnorm_spatial_persistent', 'gpu_allocator_retry_time', 'local_exe_sub_scope_limit', 'gpu_memory_limit_mb' ] - core.init_gflags([sys.argv[0]] + - ["--tryfromenv=" + ",".join(read_env_flags)]) + core.init_gflags(["--tryfromenv=" + ",".join(read_env_flags)]) core.init_glog(sys.argv[0]) # don't init_p2p when in unittest to save time. core.init_devices(not in_test) diff --git a/python/paddle/fluid/tests/unittests/test_run_fluid_by_module_or_command_line.py b/python/paddle/fluid/tests/unittests/test_run_fluid_by_module_or_command_line.py new file mode 100644 index 0000000000000000000000000000000000000000..df626dc6dded7e64c78b4eb809dc8635c19d029e --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_run_fluid_by_module_or_command_line.py @@ -0,0 +1,34 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import os +import sys + + +class TestRunFluidByModule(unittest.TestCase): + def test_module(self): + print(sys.executable) + res = os.system(sys.executable + ' -m "paddle.fluid.reader"') + self.assertEqual(res, 0) # 0 means status OK + + +class TestRunFluidByCommand(unittest.TestCase): + def test_command(self): + res = os.system(sys.executable + ' -c "import paddle.fluid"') + self.assertEqual(res, 0) # 0 means status OK + + +if __name__ == '__main__': + unittest.main()