6

I have created a module modA which I am importing in my main program. Depending on what happens in my main program (it has an interactive mode and a batch script mode), I want modA itself to import matplotlib with either the TkAgg backend or the ps backend. Is there a way for my main program to communicate information to modA to tell it how it should import matplotlib?

To clarify the situation:

The main program:

#if we are in interactive mode
#import modA which imports matplotlib using TkAgg backend
#else
#import modA which imports matplotlib using the ps backend

Module modA:

#import matplotlib
#matplotlib.use('ps') or matplotlib.use('TkAgg') (how can I do this?)
user3208430
  • 636
  • 1
  • 6
  • 14
  • Is it possible to import a module like matplotlib twice under 2 different names, ie. import matplotlib as matplotlib1 and import matplotlib as matplotlib2 and then use one or the other depending on my needs? – user3208430 Jan 23 '14 at 02:32

2 Answers2

8

Have a function in your module which will determine this.

import matplotlib

def setEnv(env):
    matplotlib.use(env)

Then in your program you can have modA.setEnv('ps') or something else based on if-else statement condition.

You do not need a conditional import here (since you are using only one external module), but it is possible to do it:

if condition:
    import matplotlib as mlib
else:
    import modifiedmatplotlib as mlib

For more information about importing modules within function see these:

Python: how to make global imports from a function

Is it possible to import to the global scope from inside a function (Python)?

Community
  • 1
  • 1
sashkello
  • 17,306
  • 24
  • 81
  • 109
  • This is a good suggestion, however one complication: once I do import matplotlib, and then call matplotlib.use() through that function, I still want to import matplotlib.pyplot (the import matplotlib and matplotlib.use() are really some kind of initialization. Is there a way around this? – user3208430 Jan 23 '14 at 02:40
  • What exactly is preventing you from importing matplotlib.pyplot independently? – sashkello Jan 23 '14 at 02:42
  • Because import matplotlib.pyplot alone will use the default back-end, and (I might be wrong here) it seems that the only way to set that back-end programmatically is to first import matplotlib and call matplotlib.use('desired_backend') – user3208430 Jan 23 '14 at 02:45
  • Ah, well, just import it after your `use()` inside that function. – sashkello Jan 23 '14 at 02:46
  • ok, so if I do that import matplotlib.pyplot in the setEnv(env) function, will it be visible in all other functions of the module as well? – user3208430 Jan 23 '14 at 02:49
  • 1
    @user3208430 Just specify it as `global` and it will be available throughout. See more info here: http://stackoverflow.com/questions/11990556/python-how-to-make-global-imports-from-a-function – sashkello Jan 23 '14 at 02:52
3

You can probably detect the way your session is started by evaluating the arguments passed to the command line:

import sys
import matplotlib

if '-i' in sys.argv:
    # program started with an interactive session
    matplotlib.use('TkAdd')
else:
    # batch session
    matplotlib.use('ps')

If not, you can use os.environ to communicate between modules:

In main:

import os
if interactive:
    os.environ['MATPLOTLIB_USE'] = 'TkAdd'
else:
    os.environ['MATPLOTLIB_USE'] = 'ps'

In modA:

import os
import matplotlib
matplotlib.use(os.environ['MATPLOTLIB_USE'])
gawel
  • 2,038
  • 14
  • 16