python sys.path问题

在我们使用python的过程中,通常我们需要导入模块,当我们使用import moudle名称时,实际上编译器就会去系统默认指定的位置寻找相应的模块。如果没能找到,就会提示没有模块存在的错误,我们可以使用打印sys.path的方法来查看系统默认查找的位置。
代码如下:

import sys


print(sys.path)
"""
['D:\\python\\scientificCalculation', 'D:\\python\\scientificCalculation', 
'D:\\software\\programSoftware\\python3.8.2\\python38.zip', 
'D:\\software\\programSoftware\\python3.8.2\\DLLs', 
'D:\\software\\programSoftware\\python3.8.2\\lib', 'D:\\software\\programSoftware\\python3.8.2', 
'D:\\software\\programSoftware\\python3.8.2\\PythonEnvironment', 
'D:\\software\\programSoftware\\python3.8.2\\PythonEnvironment\\lib\\site-packages', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages\\imglyb-0.4.1.dev0-py3.8.egg', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages\\scyjava-0.4.0-py3.8.egg', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages\\win32', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages\\win32\\lib', 
'D:\\software\\programSoftware\\python3.8.2\\lib\\site-packages\\Pythonwin']
"""

我们可以看到上述的列表中就是对应的编译器会默认查找的位置。
那么如果当我们想要导入自己使用的文件呢?如果我们自己使用的文件并不在这些默认的目录下呢?此时我们就需要为编译器添加一个查找的目录,这样编译器才会去我们指定的位置查找我们的文件。
代码如下:

import sys


sys.path.append("D:/User(origin in C)/desktop/")

这样编译器就会自动去桌面查找文件了。

如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/109013295