我在 Windows 7 上使用 Python 3.2。当我打开 Python shell 时,我如何知道当前目录是什么以及如何将其更改为我的模块所在的另一个目录?
您可以使用该os模块。
os
>>> import os >>> os.getcwd() '/home/user' >>> os.chdir("/tmp/") >>> os.getcwd() '/tmp'
但如果是关于寻找其他模块:你可以设置一个名为 的环境变量PYTHONPATH,在 Linux 下就像
PYTHONPATH
export PYTHONPATH=/path/to/my/library:$PYTHONPATH
然后,解释器也在这个地方搜索imported 模块。我猜这个名字在Windows下会是一样的,但不知道如何改变。
import
编辑
在 Windows 下:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
(取自http://docs.python.org/using/windows.html)
编辑 2
…甚至更好:使用virtualenvand virtualenv_wrapper,这将允许您创建一个开发环境,您可以在其中添加您喜欢的模块路径 ( add2virtualenv),而不会污染您的安装或“正常”工作环境。
virtualenv
virtualenv_wrapper
add2virtualenv
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html