【发布时间】:2019-11-26 21:05:19
【问题描述】:
这里是新手
我想从另一个目录的文件中导入一个类,在这种情况下:
C:\Users\jose_\Desktop\my_ref\stack_class.py,使用 from - import 语句。我在上述目录中有一个 init.py 文件,但在任何地方都找不到正确的 sintax。
C:\Users\jose_\Desktop\my_ref\stack_class.py 怎么放在导入部分?
我尝试过使用其他两种方式:
exec(open("file path on windows").read())
以及导入 sys 和 sys.path.insert
他们都工作,试图用 import 语句来做。
from C:\Users\jose_\Desktop\my_ref\stack_class.py import Stack #Error here
foo = Stack()
print(type(foo))
错误
File "C:/Users/jose_/OneDrive/Mis_Documentos/Educacion/Linkedin/Ex_Files_Python_Data_Structures_Queues/Exercise_Files/Ch02/02_02/End/main2.py", line 4
from C:\Users\jose_\Desktop\my_ref\stack_class.py import Stack
^
SyntaxError: invalid syntax
【问题讨论】:
-
查看 python 文档以获取软件包。 https://docs.python.org/3/tutorial/modules.html#packages
-
@joseag312 看看stackoverflow.com/a/15681607/7583919
-
我试过用 sys.path.append 来做,它有效,只是想用 import 语句做同样的事情。
-
@ComplicatedPhenomenom
from C.Users.jose_.Desktop.my_ref import stack_class返回No module named 'C' -
from stack_class import Stack
标签: python python-3.x windows directory python-import