首页 > 解决方案 > print something at import only

问题描述

I have a script for which I want something ("This is the script version 1.0" in the example below) to be printed only when imported (or reloaded). Basically, my script has several functions and one class, but somehow (since I upgraded to python 3.8 actually), the first message in the code is printed several time while executing functions in the class. So is there a way to check when I first import the module? The script is basically like this:

import numpy as np

print('This is the script version 1.0')

def func1():
   blah blah

def func2():
   blah blah

class Open:
   def __init__(self, others):
      blah blah

   def func3(self)
      blah blah

def func4():
   blah blah

def func5():
   blah blah

标签: python-3.ximportprinting

解决方案


推荐阅读