Is it possible to import a python file more than once in a python script because i run a loop back to my driver file in a function by using the import command but it only works once? thanks
edit: Resolved myself thanks
Is it possible to import a python file more than once in a python script because i run a loop back to my driver file in a function by using the import command but it only works once? thanks
edit: Resolved myself thanks
The easiest answer is to put the code you are trying to run inside a function like this
(inside your module that you are importing now):
def main():
# All the code that currently does work goes in here
# rather than just in the module
(The module that does the importing)
import your_module #used to do the work
your_module.main() # now does the work (and you can call it multiple times)
# some other code
your_module.main() # do the work again
While Tom Ley's answer is the correct approach, it is possible to import a module more than once, using the reload built-in.
module.py:
print "imported!"
>>> import module
imported!
>>> reload(module)
imported!
<module 'module' from 'module.pyc'>
Note that reload returns the module, allowing you to rebind it if necessary.