如何添加默认路径以在其中查找 python 脚本文件? [英] How can I add a default path to look for python script files in?

查看:27
本文介绍了如何添加默认路径以在其中查找 python 脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在弄清楚如何在 Windows 中正确设置 Python 时遇到一些麻烦.

我已经设置了 path=%path%;C:python27 ,所以我可以用 python 打开 .py 文件.我只是不知道如何更改保存目录.

例如,我将所有自定义脚本保存在 Documents/Python 目录中.这是Win7,所以没有我的文档.我希望能够在 IDLE 中键入HelloWorld.py"并让它在此文件夹中搜索任何匹配的脚本名称.不过,我一直无法弄清楚如何将此目录添加到默认的 Python 搜索路径中.

有什么想法吗?

这是一次尝试.

<预><代码>>>>导入系统>>>系统路径['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\站点包']>>>sys.path.append('C:UsersJimmyDocumentsPython')>>>你好世界回溯(最近一次调用):文件<pyshell#5>",第 1 行,在 <module>HelloWorld.py NameError:未定义名称HelloWorld"`

解决方案

import syssys.path.append(YOUR_PATH) # 或 .insert(0, YOUR_PATH) 可能会给予更高的优先级

或者设置你的 $PYTHONPATH 环境变量

I've always had a bit of trouble figuring out how to get Python set up properly in Windows.

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.

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.

Any ideas?

Here's one attempt.

>>> 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:UsersJimmyDocumentsPython') 
>>> 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

or set your $PYTHONPATH environment variable

这篇关于如何添加默认路径以在其中查找 python 脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆