Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从其他源(python)导入代码(windows 7、8、10)_Python_Python 3.x - Fatal编程技术网

从其他源(python)导入代码(windows 7、8、10)

从其他源(python)导入代码(windows 7、8、10),python,python-3.x,Python,Python 3.x,我有个问题。两个不同的代码位于同一目录中,但我无法执行以下操作: 从py_文件1导入* 考虑到这是py_file2.py,目录如下所示: New Folder > py_file1.py py_file2.py 我希望能够做到这一点: #file1 contents: def a(): print("A function in file1.") 这是: #file2 contents from py_file1 import * a() 在解释器中运行时,没有

我有个问题。两个不同的代码位于同一目录中,但我无法执行以下操作:

从py_文件1导入*

考虑到这是
py_file2.py
,目录如下所示:

New Folder >
    py_file1.py
    py_file2.py
我希望能够做到这一点:

#file1 contents:
def a():
    print("A function in file1.")
这是:

#file2 contents
from py_file1 import *
a()
在解释器中运行时,没有
ImportError

>>>
A function in file1.
>>>

看起来您当前所在的目录与存储文件的位置不同。因此,后者不在这条道路上

您应该将模块放在python的搜索路径中,或者将文件所在的目录添加到搜索路径中:

from sys import path
path.insert(1, "path/to/lib")
from py_file1 import *
a()

你是怎么遇到这个问题的,因为我无法复制。无法在ubuntu上复制。@Fiskie好吧,我在同一个文件夹中有两个python程序,我希望其中一个运行另一个-我希望py_文件2运行或导入py_文件1看起来你当前的目录与文件存储的位置不一样。因此,后者不在路径中。请列出全部信息:问题的最小可验证示例、预期输出和实际输出。我看不出你的陈述中有任何问题(也没有问题)。如果没有导入库,他不应该得到导入错误吗?他说,“当在解释器中运行时,没有
importorror
”。