[英]How do you run python scripts from other script and have their root in my root?
I have a module "B", I want to run it from a script "C", and I want to call global variables in "B", as they were in the "C" root. 我有一个模块“ B”,我想从脚本“ C”运行它,我想在“ B”中调用全局变量,就像它们在“ C”根目录中一样。 Another problem is if I imported
sys
in "B" when I run "C" it doesn't see sys
另一个问题是,如果我在运行“ C”时将“
sys
”导入“ B”,则看不到sys
# NameError: global name 'sys' is not defined #
What shall I do? 我该怎么办?
When you import a module B
(like import B
), every line in B
will be interpreted. 导入模块
B
(如import B
)时,将解释B
每一行。 I assume this is what you mean when you say you want to run it. 我认为这就是您说的要运行它时的意思。 To reference members in
B
's namespace, you can get them like: 要引用
B
的命名空间中的成员,您可以像这样获得它们:
B.something_defined_in_B
. B.something_defined_in_B
。
If you wish to use sys
explicitly in C
, you will need to import it within C
as well. 如果希望在
C
显式使用sys
,则也需要将其导入C
中。
is it in your PYTHON_PATH? 它在您的PYTHON_PATH中吗?
if not, in script C's init .py 如果不是,则在脚本C的init .py中
import os, sys
sys.path.append('/PATH/TO/MODULE/B')
then, in module C 然后,在模块C中
from B import *
something_defined_in_B()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.