简体   繁体   English

尝试导入 package 时出现 python ModuleNotFoundError

[英]python ModuleNotFoundError when trying to import a package

I have two folders on my desktop, one called 'testpackage' and another called 'testplay'.我的桌面上有两个文件夹,一个名为“testpackage”,另一个名为“testplay”。 'testpackage' contains an init .py and another file named 'numberstuff.py' (a class with two methods). 'testpackage' 包含一个init .py 和另一个名为 'numberstuff.py' 的文件(一个 class 有两种方法)。 'testplay' contains 'play_01.py', a simple script to just check I can add a package to the sys.path without physically adding it into the library in sys.path, I thought I could do this by sys.path.append(path\to\file) on python3 on windows. 'testplay' 包含'play_01.py',一个简单的脚本来检查我是否可以将 package 添加到 sys.path 而无需将其物理添加到 sys.path 中的库中,我想我可以通过 sys.path.append 做到这一点(path\to\file) 在 windows 上的 python3 上。

testplays 'play_01.py' code:测试播放“play_01.py”代码:

import sys
for i in sys.path:
    print(i,'\n')

sys.path.append('C:\\Users\\priper\\Desktop\\testpackage')
from testpackage import numberstuff


a = numberstuff.maffs()
print(a.sqrtx(3))

console return:控制台返回:

   C:\Users\priper\AppData\Local\Programs\Python\Python37\python37.zip 
C:\Users\priper\AppData\Local\Programs\Python\Python37\DLLs 
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib 
C:\Users\priper\AppData\Local\Programs\Python\Python37 
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages 
C:\Users\priper\AppData\Roaming\Python\Python37\site-packages 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32 
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32\lib 
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\Pythonwin 
C:\Users\priper\Desktop\sdnplay 
C:\Users\priper\Desktop\mypackage 
sdnplay.py 
sdnplay.py 
sdnplay.py 
sdnplay 
sdnplay 
C:\Users\priper\Desktop\sdnplay 
C:\Users\priper\Desktop\sdnplay 
C:\Users\priper\Desktop\sdnplay 
C:\Users\priper\Desktop\sdnplay 
C:\Users\priper\Desktop\testplay 
C:\Users\priper\Desktop\testpackage 
C:\Users\priper\Desktop\testpackage 

Error:错误:

ModuleNotFoundError: No module named 'testpackage'

File"C\Users\priper\Desktop\testplay\play_01.py", line 6, in <module> from testpackage import numberstuff

I can see testpackage is there in the sys.path, I just cannot figure out why it is being treated as a module and why I can't import it>我可以在 sys.path 中看到 testpackage,我只是无法弄清楚为什么它被视为一个模块以及为什么我不能导入它>

expected output is '9'.预计 output 为“9”。

The only thing I can imagine is that you have called the folder "testpackage" but the file you are trying to import has another name.我能想象的唯一一件事是您已将文件夹命名为“testpackage”,但您尝试导入的文件有另一个名称。 But since you are trying to import a file named "testpackage" you get the exception.但是由于您尝试导入名为“testpackage”的文件,因此您会遇到异常。

I have tried to reconstruct your problem but wasn't able to.我试图重建您的问题,但无法重建。

my folder/file structur looks as follows:我的文件夹/文件结构如下所示:

/full/path/to/the/parent/folder:
    - main.py
    - theFolder/
        - IAMATEST.py

The main.py主要的.py

import sys

sys.path.append('/full/path/to/the/parent/folder/theFolder')

from IAMATEST import IAMATESTClass

IAMATESTClass.calla()

The IAMTEST.py IAMTEST.py

class IAMATESTClass:
    @staticmethod
    def calla():
        print("GOT CALLED")

and when executing the main.py I get the output "GOT CALLED".在执行main.py时,我得到 output “GOT CALLED”。

Good luck!祝你好运!

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

相关问题 Python:尝试从同一 package 导入模块时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import module from the same package 尝试导入时的 ModuleNotFoundError 和 ImportError 可以 package - ModuleNotFoundError and ImportError when trying to import can package 当我尝试导入 package 时出现 ModuleNotFoundError - ModuleNotFoundError when I am trying to import package PYTHON -- ModuleNotFoundError:当我尝试导入时没有名为“playsound”的模块 package - PYTHON -- ModuleNotFoundError: No module named 'playsound' when i am trying to import package 尝试从导入的包中导入模块时出现“ModuleNotFoundError” - 'ModuleNotFoundError' when trying to import module from imported package 尝试使用软件包时出现ModuleNotFoundError - ModuleNotFoundError when trying to use package 尝试导入 Tensorflow 时出现 ModuleNotFoundError - ModuleNotFoundError when trying to import Tensorflow Python &amp; Django - 'ModuleNotFoundError: No module named 'GoalCategoryTest'' 尝试导入 TestCase - Python & Django - 'ModuleNotFoundError: No module named 'GoalCategoryTest'' when trying to import TestCase Python:尝试从文件导入模块时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import module from files Python:尝试从同一目录导入文件时出现“ModuleNotFoundError” - Python: 'ModuleNotFoundError' when trying to import file from same directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM