简体   繁体   English

python module not found error no module named

[英]python module not found error no module named

I have a few seperate pythone file and I am using them to import another py file.我有几个单独的 pythone 文件,我正在使用它们导入另一个 py 文件。 Modules that trying to import them are in seperate folder I code sample is below尝试导入它们的模块位于单独的文件夹中,我的代码示例如下

from tez.library.image_crop import ImageCrop
from tez.library.image_process import ImageProcess
from tez.library.image_features import ImageFeatures
from tez.const.application_const import ApplicationConst
from tez.library.file_operation import FileOperation

this code is in where I want to start the py file using commond line as "python samples1.py" and thrown an error as below这段代码在我想使用command line作为“python samples1.py”启动py文件的地方,并引发如下错误

Traceback (most recent call last): File "samples1.py", line 1, in from tez.library.image_crop import ImageCrop ModuleNotFoundError: No module named 'tez'回溯(最后一次调用):文件“samples1.py”,第 1 行,来自 tez.library.image_crop import ImageCrop ModuleNotFoundError: No module named 'tez'

folder structure:文件夹结构:

.tez .tez
-- library - 图书馆
---- image_crop.py ---- image_crop.py
---- image_process.py ---- image_process.py
---- image_features.py ---- image_features.py
--src --src
---- samples1.py ---- 样本1.py

Python version: 3.8 Python 版本:3.8
Pip: 20.0.2 Pip:20.0.2
Windows 10 Pro 1909 Windows 10 Pro 1909

If you are building a package called tez (and since you tried to import it I think you are).如果您正在构建一个名为 tez 的 package (并且由于您尝试导入它,我认为您是)。 Then everything with tez needs to refer to itself locally.然后 tez 的所有内容都需要在本地引用自己。 All the files in the tez package need to refer to each other with the "." tez package 中的所有文件都需要用“.”相互引用。 and ".." imports.和“..”进口。

In samples1.py:在 samples1.py 中:

from ..library.image_crop import <something>

EDIT:编辑:

It sounds like you are misunderstanding how python imports things.听起来您误解了 python 如何导入东西。 When you run "import X" in a python script, then python looks for a package named X under sys.path.当您在 python 脚本中运行“import X”时,python 会在 sys.path 下查找名为 X 的 package。 You can append to sys.path at the top of your script if you have a custom package to look for.如果您要查找自定义 package,则可以在脚本顶部将 append 指向 sys.path。

import sys
sys.path.append(<directory of tez>)

import tez

However, it is strongly recommended that you should not be importing from a file that is under the directory structure of the package name.但是,强烈建议您不要从位于 package 名称的目录结构下的文件导入。 If "examples" is a directory of examples that use the package "tez" then "examples" should be located outside the package "tez".如果“examples”是使用 package“tez”的示例目录,则“examples”应位于 package“tez”之外。 If "examples" is inside the package "tez", then "examples" should be doing local imports "with-in" the package.如果“examples”在 package“tez”内,那么“examples”应该在 package 中进行本地导入。

Getting a handle on package use can be tricky.掌握 package 的使用可能很棘手。

sample.py can't see above of src folder, but you can tell Python to do this.: sample.py看不到上面的src文件夹,但你可以告诉 Python 这样做:

import sys
import os
tez = os.path.dirname(os.path.dirname(__file__))
# __file__ is path of our file (samples.py)
# dirname of __file__ is "src" in our state
# dirname of "src" is "tez" in our state

sys.path.append(tez) # append tez to sys.path, python will look at here when you try import something

import library.image_crop # dont write "tez"

But this is not a very good design I think.但我认为这不是一个很好的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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