我的Python导入不按教程所述工作。

huangapple go评论65阅读模式
英文:

My Python imports don't work as they should according to tutorials

问题

介绍

我在导入自己的模块/包时遇到问题,所以我查找了一些教程(书面和YouTube),并试图解决我的问题。然而,没有什么真正有效的,所以我重新创建了一个教程的结构,以便逐步跟随它(sweetcode.io)。

我使用的是通过anaconda在win10机器上安装的Python 3.9.13

示例项目的结构

-- 教程
   |-- 子文件夹1
   |    |-- 子文件夹2
   |    |   |-- __init__.py
   |    |   |-- student.py
   |    |   `-- user.py
   |    |-- __init__.py
   |    `-- item.py
   |-- 子文件夹3
   |   |-- __init__.py
   |   |-- accounts.py
   |   `-- registration.py
   |-- __init__.py
   `-- security.py

我能够按照教程一直进行,直到他们谈到从student.py导入模块security.py的部分。然而,一旦我尝试这样做,我就会收到错误消息。

尝试1:

student.py中的代码:

import 教程.security

powershell命令(当前在教程目录中):

python .\subfolder1\subfolder2\student.py

收到的错误消息:

Traceback (most recent call last):
  File "C:\Users\Schnetzubroot\Documents\test\subfolder1\subfolder2\student.py", line 1, in <module>
    import 教程.security
ModuleNotFoundError: No module named '教程.security'

尝试2:

如果我将student.py中的代码更改为:

from test import security

我仍然收到相同的错误消息。

尝试3:

尝试使用相对导入如下:from ... import security,我收到以下错误消息:

Traceback (most recent call last):
  File "C:\Users\Schnetzubroot\Documents\tutorial\subfolder1\subfolder2\student.py", line 1, in <module>
    from ... import security
ImportError: attempted relative import with no known parent package

如果有人能告诉我我做错了什么,我将不胜感激,因为我不知道自己做错了什么。同时,我欢迎对这篇文章的任何反馈,因为这是我在这里的第一个问题。

英文:

Introduction

I'm having problems with importing my own modules/packages so I searched up some tutorials (written and youtube) and was trying to solve my problems. However nothing really worked so I recreated the structure of a tutorial to follow it step by step (sweetcode.io).

I'm using Python 3.9.13 installed via anaconda on a win10 machine

Structure of the sample project

-- tutorial
   |-- subfolder1
   |    |-- subfolder2
   |    |   |-- __init__.py
   |    |   |-- student.py
   |    |   `-- user.py
   |    |-- __init__.py
   |    `-- item.py
   |-- subfolder3
   |   |-- __init__.py
   |   |-- accounts.py
   |   `-- registration.py
   |-- __init__.py
   `-- security.py

I could follow the tutorial up to the part where they talk about importing a module backwards, so say from student.py import the module security.py. However as soon as I try to do that I get an error message.

Attempt 1:

code in student.py:

import tutorial.security

powershell command (currently inside the tutorial directory):

python .\subfolder1\subfolder2\student.py

error received:

Traceback (most recent call last):
  File &quot;C:\Users\Schnetzubroot\Documents\test\subfolder1\subfolder2\student.py&quot;, line 1, in &lt;module&gt;
    import tutorial.security
ModuleNotFoundError: No module named &#39;tutorial.security&#39;

Attempt 2:

if I change the code in student.py to:

from test import security

I still get the same error message

Attempt 3:

When trying to use a relative import as such: from ... import security i get the following error message:

Traceback (most recent call last):
  File &quot;C:\Users\Schnetzubroot\Documents\tutorial\subfolder1\subfolder2\student.py&quot;, line 1, in &lt;module&gt;
    from ... import security
ImportError: attempted relative import with no known parent package

If somebody could tell me what I'm doing wrong I'd really appreciate it because I don't see what I'm doing wrong.
Also any feedback to the post is appreciated as this is my first question here

答案1

得分: 2

不要使用"import tutorial.security"。而应该使用"import security",或者为了确保安全,可以使用这个

英文:

You should not use "import tutorial.security". Instead use "import security", or just to be sure use this.

答案2

得分: 1

尝试 1 和 2:

问题似乎出在根目录 "tutorial" 并没有在您的 Anaconda 中的 Python 路径上。可以在此处找到使用 Anaconda 完成此操作的说明。

尝试 3:

问题出在您试图将 Python 文件作为脚本运行(即 __name__ == "__main__")。请参阅Python 文档(6.4.2. Intra-package References)中关于此的注释。相对导入的意图似乎并不是用于 Python 脚本,而仅供内部使用。

英文:

It looks like that page is missing some key details for use as a true tutorial.

Attempt 1 & 2

The issue here seems to be that the root "tutorial" directory is not on your python path for anaconda. Instructions for doing this using Anaconda can be found here.

Attempt 3

The issue here is you are trying to run the python file as a script. (i.e. __name__ == &quot;__main__&quot;. See the Python Docs (6.4.2. Intra-package References) note on this. It appears the intent for relative imports is not to be used in an Python script, only for internal uses.

答案3

得分: 0

from tutorial import security

Otherwise, you can also add the parent folder 'tutorial' to your system path and then just import security directly, like so:

import security
import sys

sys.path.insert(0, path/to/tutorial/folder)

英文:

I believe the correct way of doing the 'from' import would be:

from tutorial import security

Otherwise, you can also add the parent folder 'tutorial' to your system path and then just import security directly, like so:

import security
import sys

sys.path.insert(0, path/to/tutorial/folder)

huangapple
  • 本文由 发表于 2023年2月19日 02:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定