从模块中导入函数的Python导入错误(ModuleNotFoundError),该模块从另一个模块中导入类

时间:2019-02-01 06:13:09

标签: python importerror

我的文件结构如下:

if($("#YourTextAreaID").val().trim().length < 1)
{
    alert("Please Enter Text...");
    return; 
}

/dir/ main.py /src/ functionsfile.py classfile.py 具有使用在functionsfile中创建的类的函数。我用

导入该类
classfile

(我也尝试导入from classfile import ClassName )。

仅使用*语句测试functionsfile中的函数,它似乎找到了类模块并使用print就好了,但是当我ClassNameimportfunctionsfile脚本的函数,它给了我错误:

main.py

我尝试同时导入两个函数:

ModuleNotFoundError: No module named 'classfile'

from functionsfile import function

我不知道为什么会这样吗?

3 个答案:

答案 0 :(得分:1)

似乎您缺少 __ init __。py 文件。将其添加到/ src /文件夹后,尝试导入。

答案 1 :(得分:1)

尝试

from src.classfile import ClassName

更改目录结构也将更好。

/dir
  /src
    /class
       classfile.py
       functionsfile.py
    main.py

答案 2 :(得分:1)

就像水瓶座所说的那样,您需要在src文件夹中创建一个空白的Unrestricted文件

并尝试将其导入为

Import-Module CosmosDB

$Guid = $([Guid]::NewGuid().ToString())
$document = @" { ... } "@

$cosmosDbContext = New-CosmosDbContext -Account $CosmosDbAccountName
-Database $DatabaseName -ResourceGroup $ResourceGroupName     New-CosmosDbDocument -Context $cosmosDbContext -CollectionId $CollectionId -DocumentBody $document -PartitionKey "$Guid"

在您的__init__.py文件中

我希望它对您有用:D