Best practices for importing into IPython

I am writing a .py file that will be regularly imported at the beginning of some of my IPython sessions in the first cells, but will also be imported from other non-interactive sessions, since it contains functions that can be run in a package in non-interactive mode.

This is basically a module that contains many classes and functions that are very common.

Since I use IPython with the option --pylab=inline, the numpy functions as well as the matplotlib functions are already imported, but when you start in batch mode with a simple, python mymodule.pyyou need to specially import the numpy functions.

In the end, I came up with dual imports during an IPython session, which I really dislike.

What is the best practice in this case? Doesn't import modules twice in bad practice?

+4
source share
1 answer

Re-importing is not a problem. No matter how many times a module is imported into a program, Python will run its code only once and make only one copy of the module. All imports after the first will simply refer to the already loaded module object. If you come from the background of C ++, you can imagine that modules that have hidden ones include security devices.

+4
source

Source: https://habr.com/ru/post/1529777/


All Articles