Python 模块导入问题

发布于 2024-09-14 06:45:09 字数 331 浏览 2 评论 0原文

我运行 Windows 7,并且可以导入内置模块,但是当我保存自己的脚本并尝试在 IDLE 中导入它时,收到一条错误消息,指出该模块不存在。

我使用通过单击 Python Shell 中的“文件”和“新窗口”找到的 Python 文本编辑器。我将其作为 .py 文件保存在我在 Python 目录中创建的 Module 文件夹中。但是,每当我在 IDLE 中输入 import module_name 时,它​​都会说该模块不存在。

我做错了什么,或者没做什么?我尝试过导入 module_name、导入 module_name.py、python module_name、python module_name.py

I run Windows 7, and I can import built-in modules, but when I save my own script and try to import it in IDLE, I get an error saying that the module doesn't exist.

I use the Python text editor found by clicking "File" and "New Window" from the Python Shell. I save it as a .py file within a Module folder I created within the Python directory. However, whenever i type import module_name in IDLE, it says that the module doesn't exist.

What am I doing wrong, or not doing? I've tried import module_name, import module_name.py, python module_name, python module_name.py

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

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

发布评论

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

评论(5

口干舌燥 2024-09-21 06:45:10

尝试将模块的路径添加到 sys.path 变量:

import sys

sys.path.append(pathToModule)

Try to add the module's path to sys.path variable:

import sys

sys.path.append(pathToModule)

冷情妓 2024-09-21 06:45:10

导入您自己的模块的方法是将文件放在您的程序的目录中并使用以下代码:

from Module import *

其中 Module 是您的模块的名称。

The way to import your own modules is to place the file in the directory of your program and use the following code:

from Module import *

where Module is the name of your module.

百善笑为先 2024-09-21 06:45:10

您将创建的模块存储在哪里?创建模块时,我会执行以下操作:

  1. 在 python 文件夹的 lib\sitepackages 文件夹中创建一个文件夹,例如:myModules
  2. 在此文件夹中保存一个名为 init.py 的空白 python 文件。该文件不必包含任何语法。稍后,您可以根据需要在此文件中设置模块要求。
  3. 将您的模块保存到此文件夹中,例如:myGamesModule.py

在空闲(或您使用的任何 python 连接的 IDE)中键入:

from myModules import myGamesModule
或者
from myModules import myGamesModule as myGmMod

这应该导入你的模块,然后你可以调用类等
ex myGmMod.Player()

我确信这是正确的,因为我刚刚完成了它并且没问题。

Where are you storing the module you created? When I create a module, I do the following:

  1. Create a folder in the lib\sitepackages folder in your python folder ex: myModules
  2. Save a blank python file named init.py in this folder. The file does not have to contain any syntax. Later on, you can set module requirements in this file if you wish.
  3. Save your module into this folder ex: myGamesModule.py

In idle (or whichever python connected IDE you use) type:

from myModules import myGamesModule
or
from myModules import myGamesModule as myGmMod

That should import your module and then you can call the classes etc
ex myGmMod.Player()

I am sure that this is correct, as I have just done it and it was ok.

他是夢罘是命 2024-09-21 06:45:10

将名为 init.py 的空白文件添加到模块文件夹中。这告诉 python 该文件夹是一个包并允许您从中导入
文件名应该是

__init__.py

stackoverflow 不断格式化我的答案并搞乱事情。这是单词 init 两侧各有 2 个下划线

add a blank file called init.py to the module folder. this tells python that the folder is a package and allows you to import from it
the file name should be

__init__.py

stackoverflow keeps formatting my answers and messing thngs up. Thats 2 underscores on each side ofv the word init

娜些时光,永不杰束 2024-09-21 06:45:09

Python 使用 PYTHONPATH 环境变量来定义导入模块时应查看的文件夹列表。您的文件夹很可能不是 PYTHONPATH

Python uses PYTHONPATH environment variable to define a list of folders which should be looked at when importing modules. Most likely your folder is not PYTHONPATH

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