Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I know it can be done, but I never remember how.

How can you re import a module in python? The scenario is as follows: I import a module interactively and tinker with it, but then I face an error. I fix the error in the .py file and then I want to re-import the fixed module without quitting python. 

How can I do it?

2 Answers

0 votes
by (106k points)

You can re-import a module in python, by using the importlib and its function reload. 

import importlib

importlib.reload(some_module)

If you are using python 3.2 or 3.3 you can use the following code:-

import imp

imp.reload(module)

0 votes
by (1.3k points)

You can re import a module by using importlib, which gives you an ability to reload a module.

  • Import sample_module

  • Make changes to sample_module.py

  • Reload the sample_modulde to reflect the changes made

    • Import importlib

    • importlib.reload(sample_module)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...