簡體   English   中英

Python 用戶定義模塊在哪里導入系統模塊

[英]Python user defined modules where to import system modules

我創建了一個模塊: test.py 里面看起來像:

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) 要在某處使用它作為import testfrom test import test_img ,我在哪里import matplotlib.pyplot as plt 是不是像現在一樣在test.py里面?

(2) 在這種情況下, plot_img是否嵌套在test_img中是否重要,如果它只在test_img

(3) 我在哪里聲明常量N有關系嗎(這是一個例子,我有很多常量)? 如果它在plot_img中使用怎么辦,我在哪里聲明它?

  1. 是的,您可以按原樣導入 matplotlib。
  2. 它應該可以正常工作。
  3. 將常量放在任何使用它們的地方。 如果它們僅用於一個函數,請考慮將它們放入其中。 但是你現在擁有的一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM