Python 错误“ImportError:没有命名的模块”

发布于 2024-07-09 04:12:56 字数 831 浏览 13 评论 0原文

Python 安装在本地目录中。

我的目录树如下所示:

(local directory)/site-packages/toolkit/interface.py

我的代码在这里:

(local directory)/site-packages/toolkit/examples/mountain.py

要运行该示例,我编写了 python mountain.py,并且在我的代码中:

from toolkit.interface import interface

我收到错误:

Traceback (most recent call last):
  File "mountain.py", line 28, in ?
    from toolkit.interface import interface
ImportError: No module named toolkit.interface

我已经检查了 < code>sys.path 那里有目录 /site-packages。 另外,我在工具包文件夹中有文件 __init__.py.bin 来向 Python 表明这是一个包。 我的示例目录中还有一个 __init__.py.bin

我不知道为什么Python在sys.path中找不到该文件。 有任何想法吗? 会不会是权限问题? 我需要一些执行权限吗?

Python is installed in a local directory.

My directory tree looks like this:

(local directory)/site-packages/toolkit/interface.py

My code is in here:

(local directory)/site-packages/toolkit/examples/mountain.py

To run the example, I write python mountain.py, and in the code I have:

from toolkit.interface import interface

And I get the error:

Traceback (most recent call last):
  File "mountain.py", line 28, in ?
    from toolkit.interface import interface
ImportError: No module named toolkit.interface

I have already checked sys.path and there I have the directory /site-packages. Also, I have the file __init__.py.bin in the toolkit folder to indicate to Python that this is a package. I also have a __init__.py.bin in the examples directory.

I do not know why Python cannot find the file when it is in sys.path. Any ideas? Can it be a permissions problem? Do I need some execution permission?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

大姐,你呐 2024-07-16 04:12:56

根据您对 orip 帖子的评论,我猜发生了这样的事情:

  1. 您在 Windows 上编辑了 __init__.py
  2. Windows 编辑器添加了一些非打印内容,可能是一个回车符(Windows 中的行尾是 CR/LF;在 unix 中它只是 LF),或者可能是 CTRL-Z(Windows 文件结束符)。
  3. 您使用 WinSCP 将文件复制到您的 UNIX 机器上。
  4. WinSCP 想:“这不是基本文本;我将添加一个 .bin 扩展名来表示二进制数据。”
  5. 缺少 __init__.py (现在称为 __init__.py.bin)意味着 python 不将工具包理解为一个包。
  6. 您在适当的目录中创建 __init__.py 并且一切正常......?

Based on your comments to orip's post, I guess this is what happened:

  1. You edited __init__.py on windows.
  2. The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
  3. You used WinSCP to copy the file to your unix box.
  4. WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
  5. The missing __init__.py (now called __init__.py.bin) means python doesn't understand toolkit as a package.
  6. You create __init__.py in the appropriate directory and everything works... ?
行雁书 2024-07-16 04:12:56

当我在 LPTHW 做这个练习时,我遇到了非常相似的事情; 我永远无法让 Python 识别出我调用的目录中有文件。 但我最终让它发挥作用。 我所做的以及我建议的就是尝试以下操作:(

注意:从您最初的帖子中,我假设您正在使用基于 *NIX 的计算机并从命令行运行某些内容,因此此建议是针对该情况量身定制的由于我运行 Ubuntu,这就是我所做的)

  1. 将目录 (cd) 更改为文件所在目录的上方目录。 在本例中,您尝试运行 mountain.py 文件,并尝试调用 toolkit.interface.py 模块,它们位于不同的目录中。 在这种情况下,您将转到包含这两个文件的路径的目录(或者换句话说,这两个文件的路径共享的最近的目录)。 在本例中是 toolkit 目录。

  2. 当您位于 toolkit 目录中时,在命令行中输入以下代码行:

    导出 PYTHONPATH=.

    这会将您的 PYTHONPATH 设置为“.”,这基本上意味着您的 PYTHONPATH 现在将在您当前所在的目录中查找任何被调用的文件(更重要的是,在子目录分支中< /em> 您所在的目录。因此,它不仅会查找当前目录,还会查找当前目录中的所有目录。


  3. 在上述步骤中设置 PYTHONPATH 后,从当前目录(toolkit 目录)运行模块。 Python 现在应该找到并加载您指定的模块。

I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:

(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)

  1. Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the mountain.py file, and trying to call the toolkit.interface.py module, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is the toolkit directory.

  2. When you are in the toolkit directory, enter this line of code on your command line:

    export PYTHONPATH=.

    This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).

  3. After you've set your PYTHONPATH in the step above, run your module from your current directory (the toolkit directory). Python should now find and load the modules you specified.

追我者格杀勿论 2024-07-16 04:12:56

(local directory)/site-packages/toolkit

__init__.py

? 要使 import 遍历目录,每个目录都必须有一个 __init__.py 文件。

Does

(local directory)/site-packages/toolkit

have a __init__.py?

To make import walk through your directories every directory must have a __init__.py file.

攒眉千度 2024-07-16 04:12:56

确保 PYTHONPATH 配置正确,尤其是它具有以下格式:(

 .:/usr/local/lib/python

注意开头的 .: ,以便它也可以在当前目录中搜索。)

在 *nix 上,还要 也可能位于其他位置,具体取决于版本:

 .:/usr/lib/python
 .:/usr/lib/python2.6
 .:/usr/lib/python2.7 and etc.

On *nix, also make sure that PYTHONPATH is configured correctly, especially that it has this format:

 .:/usr/local/lib/python

(Mind the .: at the beginning, so that it can search on the current directory, too.)

It may also be in other locations, depending on the version:

 .:/usr/lib/python
 .:/usr/lib/python2.6
 .:/usr/lib/python2.7 and etc.
帝王念 2024-07-16 04:12:56

您正在阅读这个答案,表明您的 __init__.py 位于正确的位置,您已经安装了所有依赖项,但仍然收到 ImportError

我遇到了类似的问题,除了我的程序在使用 PyCharm 运行时运行良好,但当我从终端运行它时出现上述错误。 经过进一步挖掘,我发现 PYTHONPATH 没有项目目录的条目。 因此,我设置了 PYTHONPATH per Import 语句适用于 PyCharm,但不适用于终端

export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)

还有另一种方法使用 sys.path 来执行此操作:

import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')

您可以根据您希望搜索项目的顺序使用 insert/append。

You are reading this answer says that your __init__.py is in the right place, you have installed all the dependencies and you are still getting the ImportError.

I was facing a similar issue except that my program would run fine when ran using PyCharm but the above error when I would run it from the terminal. After digging further, I found out that PYTHONPATH didn't have the entry for the project directory. So, I set PYTHONPATH per Import statement works on PyCharm but not from terminal:

export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)

There's another way to do this using sys.path as:

import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')

You can use insert/append based on the order in which you want your project to be searched.

‖放下 2024-07-16 04:12:56

使用 PyCharm(JetBrains 套件的一部分),您需要将脚本目录定义为源:
右键单击> 将目录标记为 > 来源根

Using PyCharm (part of the JetBrains suite) you need to define your script directory as Source:
Right Click > Mark Directory as > Sources Root

白云不回头 2024-07-16 04:12:56

我解决了我自己的问题,我会写一个错误的地方和解决方案的总结:

该文件需要准确地调用__init__.py。 如果扩展名不同,例如在我的例子中 .py.bin,那么 Python 就无法在目录中移动,然后就无法找到模块。 要编辑文件,您需要使用 Linux 编辑器,例如 vinano。 如果您使用 Windows 编辑器,这将写入一些隐藏字符。

影响它的另一个问题是我通过 root 安装了另一个 Python 版本,因此如果有人使用本地安装的 python,请确保运行程序的 Python 安装是本地 Python。 要检查这一点,只需执行 which python,然后查看可执行文件是否位于本地目录中。 如果不是,请更改路径,但请确保本地 Python 目录早于其他 Python 目录。

I solved my own problem, and I will write a summary of the things that were wrong and the solution:

The file needs to be called exactly __init__.py. If the extension is different such as in my case .py.bin then Python cannot move through the directories and then it cannot find the modules. To edit the files you need to use a Linux editor, such as vi or nano. If you use a Windows editor this will write some hidden characters.

Another problem that was affecting it was that I had another Python version installed by the root, so if someone is working with a local installation of python, be sure that the Python installation that is running the programs is the local Python. To check this, just do which python, and see if the executable is the one that is in your local directory. If not, change the path, but be sure that the local Python directory is before than the other Python.

橪书 2024-07-16 04:12:56

对我来说,这确实是一件愚蠢的事情。 我使用 pip3 install 安装了该库,但将我的程序运行为 python program.py,而不是 python3 program.py

For me, it was something really stupid. I installed the library using pip3 install but was running my program as python program.py as opposed to python3 program.py.

温馨耳语 2024-07-16 04:12:56

一个简单的解决方案是使用 python -m pip install 而不是 pip install 安装模块
如果受到管理员限制,您可以使用 sudo

an easy solution is to install the module using python -m pip install <library-name> instead of pip install <library-name>
you may use sudo in case of admin restrictions

我也只是我 2024-07-16 04:12:56

要将目录标记为包,您需要一个名为 __init__.py 的文件。

To mark a directory as a package you need a file named __init__.py.

深海里的那抹蓝 2024-07-16 04:12:56

对于所有仍然有这个问题的人。 我相信 Pycharm 会与导入混淆。 对我来说,当我写“从名称空间导入某些内容”时,前一行会用红色下划线表示,表明存在错误,但可以工作。 然而,“from .namespace import Something”没有下划线,但也不起作用。

尝试

try:
    from namespace import something 
except NameError:
    from .namespace import something

To all those who still have this issue. I believe Pycharm gets confused with imports. For me, when i write 'from namespace import something', the previous line gets underlined in red, signaling that there is an error, but works. However ''from .namespace import something' doesn't get underlined, but also doesn't work.

Try

try:
    from namespace import something 
except NameError:
    from .namespace import something
浪推晚风 2024-07-16 04:12:56

是的。 您需要该目录包含 __init__.py 文件,该文件是初始化包的文件。 在这里,看看这个

需要 __init__.py 文件才能使 Python 将目录视为包含包; 这样做是为了防止具有通用名称(例如字符串)的目录无意中隐藏稍后出现在模块搜索路径上的有效模块。 最简单的情况下,__init__.py 可以只是一个空文件,但它也可以执行包的初始化代码或设置 __all__ 变量,稍后介绍。

Yup. You need the directory to contain the __init__.py file, which is the file that initializes the package. Here, have a look at this.

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

翻了热茶 2024-07-16 04:12:56

如果您尝试了上面提供的所有方法但都失败了,则可能您的模块与内置模块同名。 或者,文件夹中存在同名的模块,该模块在 sys.path 中的优先级高于您的模块。

要进行调试,请说出您的 from foo.bar import baz 投诉 ImportError: No module named bar。 更改为 import foo; print foo,这将显示foo的路径。 是你所期望的吗?

如果没有,请重命名 foo 或使用 绝对导入

If you have tried all methods provided above but failed, maybe your module has the same name as a built-in module. Or, a module with the same name existing in a folder that has a high priority in sys.path than your module's.

To debug, say your from foo.bar import baz complaints ImportError: No module named bar. Changing to import foo; print foo, which will show the path of foo. Is it what you expect?

If not, Either rename foo or use absolute imports.

煞人兵器 2024-07-16 04:12:56

我的两分钱:

在此处输入图像描述

吐槽:

Traceback (most recent call last):
      File "bash\bash.py", line 454, in main
        import bosh
      File "Wrye Bash Launcher.pyw", line 63, in load_module
        mod = imp.load_source(fullname,filename+ext,fp)
      File "bash\bosh.py", line 69, in <module>
        from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
    ImportError: No module named RecordGroups

这让我很困惑 - 浏览了建议丑陋的 syspath 的帖子和帖子黑客(如你所见,我的 __init__.py 都在那里)。 事实证明 game/oblivion.py 和 game/oblivion 混淆了 python
它吐出相当无用的“没有名为 RecordGroups 的模块”。 我对解决方法和/或记录此(同名)行为的链接感兴趣 -> 编辑(2017.01.24) - 看看 如果我有一个模块和一个包会怎样同名? 有趣的是,通常优先,但显然我们的启动器违反了这一点。

编辑(2015.01.17):我没有提到我们使用 自定义启动器此处进行了剖析

My two cents:

enter image description here

Spit:

Traceback (most recent call last):
      File "bash\bash.py", line 454, in main
        import bosh
      File "Wrye Bash Launcher.pyw", line 63, in load_module
        mod = imp.load_source(fullname,filename+ext,fp)
      File "bash\bosh.py", line 69, in <module>
        from game.oblivion.RecordGroups import MobWorlds, MobDials, MobICells, \
    ImportError: No module named RecordGroups

This confused the hell out of me - went through posts and posts suggesting ugly syspath hacks (as you see my __init__.py were all there). Well turns out that game/oblivion.py and game/oblivion was confusing python
which spit out the rather unhelpful "No module named RecordGroups". I'd be interested in a workaround and/or links documenting this (same name) behavior -> EDIT (2017.01.24) - have a look at What If I Have a Module and a Package With The Same Name? Interestingly normally packages take precedence but apparently our launcher violates this.

EDIT (2015.01.17): I did not mention we use a custom launcher dissected here.

递刀给你 2024-07-16 04:12:56

对我来说,将文件作为模块运行有帮助。

而不是

python myapp/app.py

使用

python -m myapp.app

它不完全相同,但在某些情况下它可能是更好的方法。

For me, running the file as a module helped.

Instead of

python myapp/app.py

using

python -m myapp.app

It's not exactly the same but it might be a better approach in some cases.

治碍 2024-07-16 04:12:56
  1. 您必须将文件 __ init__.py 放在您要导入的文件所在的同一目录中。
  2. 您不能尝试导入具有相同名称且来自 PYTHONPATH 上配置的 2 个文件夹的文件。

例如:
/etc/environment

PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2

/opt/folder1/foo

/opt/folder2/foo

并且,如果您尝试导入 foo 文件,python 将不知道您想要哪一个。

从 foo 导入... >>> 导入错误:没有名为 foo 的模块

  1. You must have the file __ init__.py in the same directory where it's the file that you are importing.
  2. You can not try to import a file that has the same name and be a file from 2 folders configured on the PYTHONPATH.

eg:
/etc/environment

PYTHONPATH=$PYTHONPATH:/opt/folder1:/opt/folder2

/opt/folder1/foo

/opt/folder2/foo

And, if you are trying to import foo file, python will not know which one you want.

from foo import ... >>> importerror: no module named foo

小帐篷 2024-07-16 04:12:56

通过编写 print (sys.path) 解决了我的问题,并发现尽管进行了全新安装,但 python 仍在使用过时的软件包。 删除这些使得 python 自动使用正确的包。

Fixed my issue by writing print (sys.path) and found out that python was using out of date packages despite a clean install. Deleting these made python automatically use the correct packages.

荒人说梦 2024-07-16 04:12:56

就我而言,问题是我链接到 debug python & boost::Python,要求扩展名是 FooLib_d.pyd,而不仅仅是 FooLib.pyd; 重命名文件或更新 CMakeLists.txt 属性修复了该错误。

In my case, the problem was I was linking to debug python & boost::Python, which requires that the extension be FooLib_d.pyd, not just FooLib.pyd; renaming the file or updating CMakeLists.txt properties fixed the error.

下壹個目標 2024-07-16 04:12:56

就我而言,因为我使用 PyCharm,PyCharm 为项目文件夹中的每个项目创建一个“venv”,但它只是 python 的迷你环境。 虽然您已经在Python中安装了所需的库,但在您的自定义项目“venv”中,它不可用。 这就是 PyCharm 中出现“ImportError: No module name xxxxxx”的真正原因。
要解决此问题,您必须通过以下步骤将库添加到项目自定义环境中:

  • 在 PyCharm 中,从菜单“文件”->“设置”
  • 对话框中的“设置”,项目:XXXProject->项目解释器
  • 单击“添加”按钮,它将显示“可用包”对话框
  • 搜索您的库,单击“安装包”
  • 然后,您需要的所有包将安装在项目自定义“venv”文件夹中。

设置对话框

享受吧。

In my case, because I'm using PyCharm and PyCharm create a 'venv' for every project in project folder, but it is only a mini env of python. Although you have installed the libraries you need in Python, but in your custom project 'venv', it is not available. This is the real reason of 'ImportError: No module named xxxxxx' occurred in PyCharm.
To resolve this issue, you must add libraries to your project custom env by these steps:

  • In PyCharm, from menu 'File'->Settings
  • In Settings dialog, Project: XXXProject->Project Interpreter
  • Click "Add" button, it will show you 'Available Packages' dialog
  • Search your library, click 'Install Package'
  • Then, all you needed package will be installed in you project custom 'venv' folder.

Settings dialog

Enjoy.

萌无敌 2024-07-16 04:12:56

Linux:导入的模块位于 /usr/local/lib/python2.7/dist-packages

如果您使用的是用 C 编译的模块,请不要忘记在 sudo setup.py 之后 chmod .so 文件安装。

sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so

Linux: Imported modules are located in /usr/local/lib/python2.7/dist-packages

If you're using a module compiled in C, don't forget to chmod the .so file after sudo setup.py install.

sudo chmod 755 /usr/local/lib/python2.7/dist-packages/*.so
相思故 2024-07-16 04:12:56

我的问题是,我将带有 __init__.py 文件的目录添加到 PYTHONPATH,而实际上我需要添加其父目录。

My problem was that I added the directory with the __init__.py file to PYTHONPATH, when actually I needed to add its parent directory.

拍不死你 2024-07-16 04:12:56

如果您使用安装脚本/实用程序(例如setuptools)来部署您的包,请不要不要忘记将相应的文件/模块添加到安装程序中。


如果支持,请使用 find_packages() 或类似方法自动将新包添加到安装脚本中。 这绝对会让您免于头痛,尤其如果您将项目搁置一段时间然后稍后添加一些内容。

import setuptools

setuptools.setup(
    name="example-pkg",
    version="0.0.1",
    author="Example Author",
    author_email="[email protected]",
    description="A small example package",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

(示例取自 setuptools 文档

If you are using a setup script/utility (e.g. setuptools) to deploy your package, don't forget to add the respective files/modules to the installer.


When supported, use find_packages() or similar to automatically add new packages to the setup script. This will absolutely save you from a headache, especially if you put your project aside for some time and then add something later on.

import setuptools

setuptools.setup(
    name="example-pkg",
    version="0.0.1",
    author="Example Author",
    author_email="[email protected]",
    description="A small example package",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

(Example taken from setuptools documentation)

2024-07-16 04:12:56

我遇到了同样的问题(Python 2.7 Linux),我找到了解决方案,我想分享它。 就我而言,我的结构如下:

Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py

在“main.py”中,我尝试了以下所有组合,但均未成功:

from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...

解决方案比我想象的要简单得多。 我将文件夹“Booklet”重命名为“booklet”,就是这样。 现在,Python 可以通过在“main.py”中使用代码正常导入类问题:

from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass

由此我可以得出结论,像“booklet”这样的包名称(文件夹)必须从小写字母开头,否则 Python 会将其与类名称混淆,并且文件名。

显然,这不是您的问题,但是 John Fouhy 的回答非常好,并且该线程几乎包含任何可能导致此问题的内容。 所以,这是另一件事,我希望这可以帮助其他人。

I had the same problem (Python 2.7 Linux), I have found the solution and i would like to share it. In my case i had the structure below:

Booklet
-> __init__.py
-> Booklet.py
-> Question.py
default
-> __init_.py
-> main.py

In 'main.py' I had tried unsuccessfully all the combinations bellow:

from Booklet import Question
from Question import Question
from Booklet.Question import Question
from Booklet.Question import *
import Booklet.Question
# and many othet various combinations ...

The solution was much more simple than I thought. I renamed the folder "Booklet" into "booklet" and that's it. Now Python can import the class Question normally by using in 'main.py' the code:

from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass

From this I can conclude that Package-Names (folders) like 'booklet' must start from lower-case, else Python confuses it with Class names and Filenames.

Apparently, this was not your problem, but John Fouhy's answer is very good and this thread has almost anything that can cause this issue. So, this is one more thing and I hope that maybe this could help others.

苦行僧 2024-07-16 04:12:56

在 Linux 服务器中尝试 dos2unix script_name

(使用命令 find . -name '*.pyc' -deletepyc 文件) >)

如果您在 Windows 上处理脚本,则重新运行

In linux server try dos2unix script_name

(remove all (if there is any) pyc files with command find . -name '*.pyc' -delete)

and re run in the case if you worked on script on windows

云巢 2024-07-16 04:12:56

就我而言,我使用 sys.path.insert() 导入本地模块,并从不同的库中获取未找到模块。 我必须将 sys.path.insert() 放在报告找不到模块的导入下面。 我想最好的做法是将 sys.path.insert() 放在导入的底部。

In my case, I was using sys.path.insert() to import a local module and was getting module not found from a different library. I had to put sys.path.insert() below the imports that reported module not found. I guess the best practice is to put sys.path.insert() at the bottom of your imports.

我一直都在从未离去 2024-07-16 04:12:56

我发现更改别名文件夹 (Mac) 的名称(通过 GUI)可能会导致加载模块出现问题。 如果原文件夹名称发生更改,请重新制作符号链接。 我不确定这种行为有多普遍,但调试起来很令人沮丧。

I've found that changing the name (via GUI) of aliased folders (Mac) can cause issues with loading modules. If the original folder name is changed, remake the symbolic link. I'm unsure how prevalent this behavior may be, but it was frustrating to debug.

梦归所梦 2024-07-16 04:12:56

如果你的截止日期很紧,而其他一切都失败了:

import sys
import os
wd = '/path/to/current/script/'
sys.path.append(wd)
os.chdir(wd)
print(os.getcwd())
print(sys.path)

If you're on a tight deadline and all else fails:

import sys
import os
wd = '/path/to/current/script/'
sys.path.append(wd)
os.chdir(wd)
print(os.getcwd())
print(sys.path)
灯角 2024-07-16 04:12:56

在遇到同样的问题后,我发现我的解决方案是从项目中删除所有 pyc 文件,似乎这些缓存的文件以某种方式导致了此错误。

我发现执行此操作的最简单方法是导航到 Windows 资源管理器中的项目文件夹并搜索 *.pyc,然后选择全部 (Ctrl+A)并删除它们(Ctrl+X)。

我可能可以通过删除特定的 pyc 文件来解决我的问题,但我从未尝试过这个

After just suffering the same issue I found my resolution was to delete all pyc files from my project, it seems like these cached files were somehow causing this error.

Easiest way I found to do this was to navigate to my project folder in Windows explorer and searching for *.pyc, then selecting all (Ctrl+A) and deleting them (Ctrl+X).

Its possible I could have resolved my issues by just deleting the specific pyc file but I never tried this

风透绣罗衣 2024-07-16 04:12:56

我遇到了同样的问题:导入错误。 此外,该库已 100% 正确安装。 问题的根源是我的PC上安装了3版本的python(anaconda包)。 这就是为什么库没有安装在正确的位置。 之后,我只是在 IDE PyCharm 中更改为正确的 python 版本。

I faced the same problem: Import error. In addition the library've been installed 100% correctly. The source of the problem was that on my PC 3 version of python (anaconda packet) have been installed). This is why the library was installed no to the right place. After that I just changed to the proper version of python in the my IDE PyCharm.

沉鱼一梦 2024-07-16 04:12:56

我有同样的错误。 这是由于有人在与我的脚本相同的文件夹中创建了一个文件夹,该文件夹的名称与我从其他地方导入的模块冲突。 它没有导入外部模块,而是查看了该文件夹的内部,该文件夹显然不包含预期的模块。

I had the same error. It was caused by somebody creating a folder in the same folder as my script, the name of which conflicted with a module I was importing from elsewhere. Instead of importing the external module, it looked inside this folder which obviously didn't contain the expected modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文