导入其他 Python 个文件 - 调用路径
Importing Other Python Files - Calling Path
我正在尝试按照@Eric Leschinski exmaple 7 解决方案来导入其他 python 文件。我相信他使用的是 Linux 基本操作系统,因为他的路径格式为 el@apollo:/home...
。我正在使用 Windows 并且不太确定如何复制他关于路径的解决方案。我不太了解 @
和 $
符号,是否需要更改它们?
D:\Echo\herp\__init__.py
:
D:\Echo\herp$ touch__init__.py
D:\Echo\herp$ ls
__init__.py
D:\Echo\herp\derp\__init__.py
:
D:\Echo\herp\derp$ touch__init__.py
D:\Echo\herp\derp$ ls
__init__.py
D:\Echo\herp\derp\yolo.py
:
def skycake():
print("SkyCake evolves to stay just beyond the cognitive reach of " + "the bulk of men. SKYCAKE!!")
D:\Echo\main.py
from herp.derp.yolo import skycake
skycake()
Traceback (most recent call last):
File "D:/Echo/main.py", line 1, in <module>
from herp.derp.yolo import skycake
File "D:\Echo\herp\__init__.py", line 1
D:\Echo\herp$ touch__init__.py
^
SyntaxError: unexpected character after line continuation character
你理解错了。
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py
el@apollo:/home...
是Linux控制台提示,说明必须在控制台执行。
touche __init__.py
是 Linux 命令 touch
创建空文件 __init__.py
.
ls
是 Linux 显示文件夹中文件的命令,它显示 __init__.py
(您在最后一行看到)
所有这些只是为了展示如何在 Linux 的控制台中创建空文件 __init__.py
。
您必须从文件中删除所有行 __init__.py
- 它们必须是空的。
但是你显示 link 问题是 11 岁,答案是 7 岁,在当前 Python 中可能不需要。您应该显示原始问题,因为它可能需要完全不同的解决方案 - 例如使用相对路径,或在 import
之前将脚本的文件夹添加到 sys.path
我正在尝试按照@Eric Leschinski exmaple 7 解决方案来导入其他 python 文件。我相信他使用的是 Linux 基本操作系统,因为他的路径格式为 el@apollo:/home...
。我正在使用 Windows 并且不太确定如何复制他关于路径的解决方案。我不太了解 @
和 $
符号,是否需要更改它们?
D:\Echo\herp\__init__.py
:
D:\Echo\herp$ touch__init__.py
D:\Echo\herp$ ls
__init__.py
D:\Echo\herp\derp\__init__.py
:
D:\Echo\herp\derp$ touch__init__.py
D:\Echo\herp\derp$ ls
__init__.py
D:\Echo\herp\derp\yolo.py
:
def skycake():
print("SkyCake evolves to stay just beyond the cognitive reach of " + "the bulk of men. SKYCAKE!!")
D:\Echo\main.py
from herp.derp.yolo import skycake
skycake()
Traceback (most recent call last):
File "D:/Echo/main.py", line 1, in <module>
from herp.derp.yolo import skycake
File "D:\Echo\herp\__init__.py", line 1
D:\Echo\herp$ touch__init__.py
^
SyntaxError: unexpected character after line continuation character
你理解错了。
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py
el@apollo:/home...
是Linux控制台提示,说明必须在控制台执行。
touche __init__.py
是 Linux 命令 touch
创建空文件 __init__.py
.
ls
是 Linux 显示文件夹中文件的命令,它显示 __init__.py
(您在最后一行看到)
所有这些只是为了展示如何在 Linux 的控制台中创建空文件 __init__.py
。
您必须从文件中删除所有行 __init__.py
- 它们必须是空的。
但是你显示 link 问题是 11 岁,答案是 7 岁,在当前 Python 中可能不需要。您应该显示原始问题,因为它可能需要完全不同的解决方案 - 例如使用相对路径,或在 import
sys.path