简体   繁体   English

无法导入我制作的包。 “模块未找到错误”

[英]Impossible to import a package I made. "ModuleNotFoundError"

I have a project organized like so:我有一个这样组织的项目:

application
 ├── app
 │   └── package
         └── __init__.py
 │       └── functions.py
 └── app2
     └── some_folder
         └── file_2.py

My "functions.py" contains a basic function:我的“functions.py”包含一个基本功能:

#functions.py
def add(x,y):
    return x+y

The file " _ init _.py" is empty文件“ _init_.py ”为空

I want to use the "add" function in my "file_2.py" file, so I write:我想在我的“file_2.py”文件中使用“添加”功能,所以我写:

#file_2.py
from application.app.package.functions import add
print(add(2,3))

But it returns an error message:但它返回一条错误消息:

ModuleNotFoundError: No module named 'application' ModuleNotFoundError:没有名为“应用程序”的模块

it is the same if i try any of these:如果我尝试以下任何一种,都是一样的:

from app.package.functions import add
from package.functions import add
from functions import add

Does anyone know where the problem comes from?有谁知道问题出在哪里? I'm doing exactly like in this tutorial so I don't understand what's wrong我做的和本教程完全一样,所以我不明白哪里出了问题

tutorial's link 教程链接

Thank you for your help谢谢您的帮助

One way to import functions.add is to import sys and use sys.path.insert() after that you can import add from functions:导入 functions.add 的一种方法是导入 sys 并使用 sys.path.insert() ,然后您可以从函数导入添加:

import sys
sys.path.insert(1, 'the/local/path/to/package')

from functions import add

print(add(1,2))

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

相关问题 当我尝试导入 package 时出现 ModuleNotFoundError - ModuleNotFoundError when I am trying to import package 无法建立连接。 ConnectionRefusedError - No connection could be made. ConnectionRefusedError 来自用户的输入是我所做的列表。 现在我该如何阅读? - Input from user is an list I made. Now how do I read it? 尝试导入 package 时出现 python ModuleNotFoundError - python ModuleNotFoundError when trying to import a package 尝试导入时的 ModuleNotFoundError 和 ImportError 可以 package - ModuleNotFoundError and ImportError when trying to import can package 如何在没有 ModuleNotFoundError 的情况下在 bash 中导入包 - How to import package in bash without ModuleNotFoundError 导入错误:ModuleNotFoundError:'block_model'不是软件包 - import error:ModuleNotFoundError: 'block_model' is not a package 我导入枕头时出现ModuleNotFoundError - ModuleNotFoundError when I `import pillow` 我不知道如何保存我制作的这个骰子游戏的分数。 我应该改变什么? - I can't figure out how to save the score on this dice game I've made. What should I change? 当我尝试导入 jupyter notebook(使用 venv)时,安装的包给出了“ModuleNotFoundError” - Installed package giving "ModuleNotFoundError" when I try to import into jupyter notebook (using venv)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM