开发者

How do you run python scripts from other script and have their root in my root?

开发者 https://www.devze.com 2022-12-22 21:48 出处:网络
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. Another problem is if I imported sys in \"B\"

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. Another problem is if I imported sys in "B" when I run "C" it doesn't see 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. 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.something_defined_in_B.

If you wish to use sys explicitly in C, you will need to import it within C as well.


is it in your PYTHON_PATH?

if not, in script C's init.py

import os, sys
sys.path.append('/PATH/TO/MODULE/B')

then, in module C

from B import *
something_defined_in_B()
0

精彩评论

暂无评论...
验证码 换一张
取 消