如何添加默认路径以查找python脚本文件? [英] How can I add a default path to look for python script files in?
问题描述
在弄清楚如何在Windows中正确设置Python时,我总是遇到一些麻烦.
I've always had a bit of trouble figuring out how to get Python set up properly in Windows.
我已经设置了path =%path%; C:\ python27,所以我可以使用python打开.py文件.我只是很难弄清楚如何更改保存目录.
I've already set up path=%path%;C:\python27 , so I'm able to open .py files with python. I'm just having trouble figuring out how to change the save directory.
例如,我将所有自定义脚本保存在Documents/Python目录中.它是Win7,所以没有我的文档".我希望能够在IDLE中键入"HelloWorld.py",并使其在此文件夹中搜索任何匹配的脚本名称.不过,我还无法弄清楚如何将此目录添加到默认的Python搜索路径中.
For instance, I save all of my custom scripts in the directory Documents/Python. It's Win7, so no My Documents. I would like to be able to type "HelloWorld.py" into IDLE and have it search this folder for any matching script names. I haven't been able to figure out how to add this directory to the default Python search path though.
有什么想法吗?
这是一次尝试.
>>> import sys
>>> sys.path
['C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
>>> sys.path.append('C:\Users\Jimmy\Documents\Python')
>>> HelloWorld.py
Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> HelloWorld.py NameError: name 'HelloWorld' is not defined `
推荐答案
import sys
sys.path.append(YOUR_PATH) # or .insert(0, YOUR_PATH) may give higher priority
或设置您的$PYTHONPATH
环境变量
这篇关于如何添加默认路径以查找python脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!