Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让自定义模块在主Python上引用导入的模块_Python_Tensorflow - Fatal编程技术网

如何让自定义模块在主Python上引用导入的模块

如何让自定义模块在主Python上引用导入的模块,python,tensorflow,Python,Tensorflow,这是我存储在util.py中的自定义模块,它将我在使用tensorflow #tf_load_image from path def load_image(image_path, image_shape, clr_normalize = True ): '''returns tensor-array on given image path and image_shape [height, width]''' import tensorflow as tf image =

这是我存储在
util.py
中的自定义模块,它将我在使用
tensorflow

#tf_load_image from path 
def load_image(image_path, image_shape, clr_normalize = True ):
    '''returns tensor-array on given image path and image_shape [height, width]'''
    import tensorflow as tf
    image = tf.read_file(image_path)
    image = tf.image.decode_jpeg(image, channels=3)
    if clr_normalize == True:
        image = tf.image.resize_images(image, image_shape) / 127.5 - 1
    else:
        image = tf.image.resize_images(image, image_shape)
    return image   
但是,这对于处理大量图像负载来说是非常低效的,因为它通常在每次我阅读“单个图像”时将
import tensorflow调用为tf

我想让这个函数从加载图像实际导入到的
main.py
的tf继承
tf
命令

import tensor flow as tf
from util import load_image

image = load_image("path_string") #where load_image no longer wraps the import tensorflow as tf in itself.

正如@MatrixTai在评论中所建议的,您可以这样制作
utils.py

import tensorflow as tf

def load_image(.........):
    ............
main.py
中,可以根据需要导入util

但这是非常低效的(…),因为它通常在每次我阅读“单个图像”时将
import tensorflow调用为tf

导入的模块在第一次导入时(我的意思是“在给定进程中第一次导入模块时”)缓存在
sys.modules
中,后续调用将从该缓存中检索已导入的模块,因此开销比您想象的要小得多。但无论如何:

我想让这个函数从main.py的tf继承tf命令,在那里load_图像实际上被导入了

唯一的方法是明确地将模块传递给函数,即:

# utils.py

def load_image(tf, ....):
    # code here using `tf`
然后

# main.py

import tensorflow as tf
from utils import load_image
im = load_image(tf, ...)
但这实际上是完全无用的:在utils.py中,只需将import语句移出函数:

# utils.py

import tensorflow as tf
def load_image(image_path, image_shape, clr_normalize = True ):
    '''returns tensor-array on given image path and image_shape [height, width]'''
    import tensorflow as tf
    image = tf.read_file(image_path)
    image = tf.image.decode_jpeg(image, channels=3)
    if clr_normalize:
        image = tf.image.resize_images(image, image_shape) / 127.5 - 1
    else:
        image = tf.image.resize_images(image, image_shape)
    return image   

FWIW,在任何其他定义(函数、类等)之前。函数内的导入只能用作循环导入的Q&D最后解决方案(这通常是您有设计问题的确定标志,实际上应该通过修复设计本身来解决)。

移动“导入tensorflow as tf”把它放在导入部分可以让你参考,这实际上与tensorflow无关。只需在
util.py
上导入一次tensorflow,然后在main.py中导入
import util
。在方法中导入模块没有多大意义,您应该在脚本顶部导入所有必需的模块。@MatrixTai我只想导入一个util而不是整个util.py,然后添加一个文件调用
load\u image.py
。或者使用
如果uuuu name_uuuu==“\uuuuu main\uuuuuu”
来分隔由其他文件作为模块导入时不希望编译的代码。