In a Python script, I would like to control the importing of a module into the main() from a sub function. Is this possible? How?
Why: I have a sub function that handles command line arguments (using argparse) and would like to import a module based on user input. Specifically, I would like to let the user specify the backend for matplotlib, which has to be set before importing matplotlib.pylab. However, I think the question has more general use.
Here is a code snippet:
def main():
args = handleCommandLine();
fig, ax = plt.subplots(1) # ERROR: plt not defined
# Snip ...
def handleCommandLine():
p = argparse.ArgumentParser()
p.add_argument('--backend', '-b', default=None, help='Specify plotting backend')
args = p.parse_args()
if args.backend != None:
matplotlib.use(args.backend) #Must occur before importing pyplot
import matplotlib.pyplot as plt #Must occur after setting backend, if desired
return args
If you'd like it to behave as though you executed import matplotlib.pyplot as plt
at the top of the module, even though you didn't, use a global:
def handleCommandLine():
p = argparse.ArgumentParser()
p.add_argument('--backend', '-b', default=None, help='Specify plotting backend')
args = p.parse_args()
if args.backend != None:
matplotlib.use(args.backend) #Must occur before importing pyplot
global plt #Style choice: Can also be placed at the top of the function
import matplotlib.pyplot as plt #Must occur after setting backend
return args
Otherwise you can pass the included library reference through a function return, in the same way you handle any other variable:
def main():
plt, args = handleCommandLine() # CHANGED HERE
fig, ax = plt.subplots(1)
# ...
def handleCommandLine():
p = argparse.ArgumentParser()
p.add_argument('--backend', '-b', default=None, help='Specify plotting backend')
args = p.parse_args()
if args.backend != None:
matplotlib.use(args.backend) #Must occur before importing pyplot
import matplotlib.pyplot as plt #Must occur after setting backend, if desired
return plt, args # CHANGED HERE
An import
statement is much like an assignment -- it assigns to a local name, unless you explicitly declare as global
. The following imports plt
into the global namespace:
def handleCommandLine():
global plt
...
import matplotlib.pyplot as plt
You can't.
I'd solve this by either returning plt
from handleCommandLine
or moving the import to main
.
将global plt
放在handleCommandLine()
的开头。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.