Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common practices for modifying Python modules

I have a Python Module test, I want to edit test and run some functions in test at the interpreter. Will Python automatically see the differences in the functions that I edit in test without reimporting? What is the appropriate word for this action?

  1. What are the best practices for reimporting/reloading/redefining?
  2. How can this be done efficiently? (It seems inefficient to highlight text with a mouse and then copy and paste).

EDIT Nothing mentioned so far is working so I will post a few more details:

A class is in my module, called Test. So I used the statement from test import Test. Now when I try the command reload(test) the interpreter tells me reload is undefined. If I do import imp then imp.reload(test) the interpreter tells me that test is undefined. What is going wrong here?

like image 356
CodeKingPlusPlus Avatar asked Jan 14 '13 22:01

CodeKingPlusPlus


People also ask

What are the different methods of important the Python module?

Python Built-in Modulesprint() and input() for I/O, Number conversion functions such as int(), float(), complex(), Data type conversions such as list(), tuple(), set(), etc.

What are Python modules give examples?

What are modules in Python? Modules refer to a file containing Python statements and definitions. A file containing Python code, for example: example.py , is called a module, and its module name would be example . We use modules to break down large programs into small manageable and organized files.


2 Answers

I suspect you're using Python 3, and all of the answerers are assuming you're using Python 2.

In Python 3, reload was moved from builtins to imp.

So, the answer is:

imp.reload(test)

For future reference, if you're using Python 3, it's almost always worth mentioning it, unless you know for a fact that it's not relevant, because many of the old-timers (read: the people you want answer from) will assume 2.7.

Meanwhile, back to your main question:

What are the best practices for reimporting/reloading/redefining?

For simple cases, reload() or imp.reload() are exactly what you want. (For super-simple cases, there's nothing wrong with copy and paste…)

But it's very easy to confuse yourself when you have, e.g., some object bar of class foo.Bar and then reload foo. It also doesn't play too well with from foo import *, which is often handy for interactive sessions.

If you can start up a new interpreter session and import the module, that's much cleaner. If you need a bunch of objects created to start playing with the module, you can temporarily add them as globals to the module, or create a wrapper module that defines them. Some IDEs can wrap all of this up so just hitting some accelerator key sequence causes it to restart the interpreter and rerun your import and all your setup stuff.

like image 185
abarnert Avatar answered Sep 30 '22 07:09

abarnert


Will Python automatically see the differences in the functions that I edit in test without reimporting?

No.

What is the appropriate word for this action?

See the builtin reload(module).

like image 40
Dolph Avatar answered Sep 30 '22 08:09

Dolph