Skip to main content
Update to use Python 3 compatible syntax
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

You could use imp.load_moduleimp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Otherwise, can always set __name__ manually:

>>> src = '''\
... if __name__ == '__main__': print ('Main!')
... else: print ('Damn', __name__)
... '''
>>> exec (src)
Main!
>>> exec (src in, {})
Damn __builtin__builtins
>>> exec (src in, {'__name__':'__main__'})
Main!

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Otherwise, can always set __name__ manually:

>>> src = '''\
... if __name__ == '__main__': print 'Main!'
... else: print 'Damn', __name__
... '''
>>> exec src
Main!
>>> exec src in {}
Damn __builtin__
>>> exec src in {'__name__':'__main__'}
Main!

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Otherwise, can always set __name__ manually:

>>> src = '''\
... if __name__ == '__main__': print('Main!')
... else: print('Damn', __name__)
... '''
>>> exec(src)
Main!
>>> exec(src, {})
Damn builtins
>>> exec(src, {'__name__':'__main__'})
Main!
added 308 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Otherwise, can always set __name__ manually:

>>> src = '''\
... if __name__ == '__main__': print 'Main!'
... else: print 'Damn', __name__
... '''
>>> exec src
Main!
>>> exec src in {}
Damn __builtin__
>>> exec src in {'__name__':'__main__'}
Main!

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Otherwise, can always set __name__ manually:

>>> src = '''\
... if __name__ == '__main__': print 'Main!'
... else: print 'Damn', __name__
... '''
>>> exec src
Main!
>>> exec src in {}
Damn __builtin__
>>> exec src in {'__name__':'__main__'}
Main!
added 203 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

You could use imp.load_module instead:

import imp

with open(mainfile) as src:
    imp.load_module('__main__', src, mainfile, (".py", "r", imp.PY_SOURCE))

This imports the file as the __main__ module, executing it.

Note that it takes an actual file object when the type is set to imp.PY_SOURCE, so you'd need to create a temporary file for this to work if your source code comes from somewhere other than a file.

Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading