简体   繁体   中英

Python user defined modules where to import system modules

I have created a module: test.py . Inside it looks like:

import matplotlib.pyplot as plt   
N = 5
def test_img(data_loc):
    #F = load images from data_loc

    def plot_img(im):
        # a bunch of other codes
        plt.imshow(im)

    for i in range(N):
        plot_img(F(i))

(1) To use it somewhere as import test or from test import test_img , where do I import matplotlib.pyplot as plt ? Is it inside test.py like it is now?

(2) Does it matter plot_img is nested inside test_img in this case, if it is only going to be used inside test_img ?

(3) Does it matter where do I declare constant N (This is an example, I have a lot of constants)? What if it is used inside plot_img , where do I declare it?

  1. Yes, you can import matplotlib as you are.
  2. It should work fine.
  3. Put your constants wherever they are used. If they're only used in one function, consider putting them inside it. But what you have now is OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM