Python IDLE 的默认工作目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15367688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default working directory for Python IDLE?
提问by HaiXin Tie
Is there a configuration file where I can set its default working directory? It currently defaults to my home directory, but I want to set it to another directory when it starts. I know I can do "import os" followed by "os.chdir("")" but that's kind of troublesome. It'd be great if there is a conf file that I can edit and change that setting, but I am unable to find it.
是否有配置文件可以设置其默认工作目录?它目前默认为我的主目录,但我想在启动时将其设置为另一个目录。我知道我可以先执行“import os”,然后再执行“os.chdir("")”,但这有点麻烦。如果有一个我可以编辑和更改该设置的 conf 文件,那就太好了,但我找不到它。
In particular, I've looked into my OS (Ubuntu)'s desktop entry '/usr/share/applications/idle-python3.2.desktop', which doesn't contain a conf file, but points to '/usr/lib/python3.2/idlelib/PyShell.py', which points to config-*.def conf files under the same folder, with 'config-main.def' being the most likely candidate. However I am unable to find where the default path is specified or how it can be changed.
特别是,我查看了我的操作系统 (Ubuntu) 的桌面条目“/usr/share/applications/idle-python3.2.desktop”,它不包含 conf 文件,但指向“/usr/ lib/python3.2/idlelib/PyShell.py',它指向同一文件夹下的 config-*.def conf 文件,其中 'config-main.def' 是最有可能的候选。但是我无法找到指定默认路径的位置或如何更改它。
It seems that the path is hard-coded in PyShell.py, though I could be wrong with my limited knowledge on Python. I will keep looking, but would appreciate it if somebody knows the answer on top of his or her head. Thanks in advance.
该路径似乎是在 PyShell.py 中硬编码的,尽管我对 Python 的了解有限,但我可能是错误的。我会继续寻找,但如果有人知道他或她头顶上的答案,我将不胜感激。提前致谢。
回答by PurityLake
It can change depending on where you installed Python. Open up IDLE, import os, then call os.getcwd()and that should tell you exactly where your IDLE is working on.
它可以根据您安装 Python 的位置而改变。打开 IDLE,import os然后调用os.getcwd(),这应该会告诉您 IDLE 的确切工作位置。
回答by HaiXin Tie
I've found a solution after looking into PyShell.py:
在查看 PyShell.py 后,我找到了一个解决方案:
- Create a python file under a preferred directory, in my case '~/.idlerc/init.py', and copy/paste the following lines:
- 在首选目录下创建一个 python 文件,在我的例子中是“~/.idlerc/init.py”,然后复制/粘贴以下几行:
import os os.chdir('<your preferred directory>')
import os os.chdir('<your preferred directory>')
- Pass "-r '~/.idlerc/init.py' " argument to the IDLE startup command, like the following (your exec location and name may vary depending on OS, etc):
- 将“-r '~/.idlerc/init.py'”参数传递给 IDLE 启动命令,如下所示(您的 exec 位置和名称可能因操作系统等而异):
/usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py
/usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py
回答by uselpa
Just use a shell script such as:
只需使用 shell 脚本,例如:
#!/bin/bash
cd /Users/pu/Projects/L-Python
/usr/bin/idle
and run that instead of stock idle. The example is on OS X, adapt to your system.
并运行它而不是闲置库存。该示例在 OS X 上,适应您的系统。
回答by Steven Choi
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
如果您使用标有“空闲(Python GUI)”的快捷链接,我实际上只是发现了最简单的答案。这是在 Windows Vista 中,所以我不知道它是否适用于其他操作系统。
1) Right-click "Properties".
1) 右键单击“属性”。
2) Select "Shortcut" tab.
2) 选择“快捷方式”选项卡。
3) In "Start In", write file path (e.g. "C:\Users...").
3) 在“Start In”中,写入文件路径(例如“C:\Users...”)。
This is also my answer here: Default save path for Python IDLE?Let me know if this works!
这也是我的答案:Python IDLE 的默认保存路径?让我知道这个是否奏效!
回答by User
One default path is specified in idlelib.IOBinding.IOBinding.dirnameor idlelib.IOBinding.IOBinding.filename
在idlelib.IOBinding.IOBinding.dirname或 中指定了一个默认路径idlelib.IOBinding.IOBinding.filename
Ubuntu
Ubuntu
So my idle-python3.desktop file in /usr/share/applications looks like this:
所以我在 /usr/share/applications 中的idle-python3.desktop 文件看起来像这样:
[Desktop Entry]
Name=IDLE (using Python-3)
Comment=Integrated Development Environment for Python (using Python-3)
Exec=python3 -c "import idlelib.IOBinding, os; idlelib.IOBinding.IOBinding.dirname='/DEFAULT/DIRECTORY';import idlelib.idle"
Icon=/usr/share/pixmaps/python3.xpm
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true
To use it you need to set /DEFAULT/DIRECTORY to your desired directory, copy it with root rights into /usr/share/applications. You can also use it for Python 2 but then you need to replace the 3s with 2s.
要使用它,您需要将 /DEFAULT/DIRECTORY 设置为您想要的目录,并以 root 权限将其复制到 /usr/share/applications 中。您也可以将它用于 Python 2,但是您需要将 3s 替换为 2s。
ConfigFiles
There are also extensions that can be loaded. These must be modules and you specify them by module name. The config files for IDLE are located in HOME/.idlerc and parsed with a configparser. I did not get further with this.
ConfigFiles
也有可以加载的扩展。这些必须是模块,您可以通过模块名称指定它们。IDLE 的配置文件位于 HOME/.idlerc 并用 .idlerc 解析configparser。我没有进一步了解这一点。
回答by Paul - Tiger Technics Ltd
I'm new to python and learning from 'Dive into Python' by mark Pilgrim (can be found online free) the answer is in chapter 2.4 - hope he doesn't mind me pasting it here as its also plugging his book and is in the GPL
我是 python 新手,从 Mark Pilgrim 的“Dive into Python”中学习(可以在网上免费找到)答案在第 2.4 章 - 希望他不介意我把它贴在这里,因为它也插入了他的书并且在通用公共许可证
Before you go any further, I want to briefly mention the library search path. Python looks in several places when you try to import a module. Specifically, it looks in all the directories defined in sys.path. This is just a list, and you can easily view it or modify it with standard list methods. (You'll learn more about lists later in this chapter.)
在继续之前,我想简单地提一下图书馆搜索路径。当您尝试导入模块时,Python 会查看多个位置。具体来说,它会查看 sys.path 中定义的所有目录。这只是一个列表,您可以使用标准列表方法轻松查看或修改它。(您将在本章后面了解有关列表的更多信息。)
Example?2.4.?Import Search Path
示例?2.4.?导入搜索路径
import sys
sys.path
sys.path.append('/my/new/path')
It's a good book I am a programmer - usually I find learning from books sends me quickly to sleep - not the case here ....
这是一本好书,我是一名程序员——通常我发现从书本中学习会让我很快入睡——这里不是这样......
回答by kalle123
All I had to do here (Linux Mint 18.2 Xfce) ...
我必须在这里做的一切(Linux Mint 18.2 Xfce)......


Just add path in line "working directory" = "Arbeitsverzeichnis"
只需在行中添加路径 "working directory" = "Arbeitsverzeichnis"
回答by Peter S
Here's a way to reset IDLE's default working directory for MacOS if you launch Idle as an application by double-clicking it. You need a different solution if you launch Idle from a command line in Terminal. This solution is a permanent fix. You don't have to rechange the directory everytime you launch IDLE. I wish it were easier.
如果您通过双击将 Idle 作为应用程序启动,这里有一种为 MacOS 重置 IDLE 的默认工作目录的方法。如果从终端中的命令行启动空闲,则需要不同的解决方案。此解决方案是永久性修复。您不必每次启动 IDLE 时都重新更改目录。我希望它更容易。
The idea is to edit a resource file inside of the IDLE package in Applications.
这个想法是在 Applications 中的 IDLE 包内编辑资源文件。
Start by finding the the file. In Finder, go to IDLE in Applications (in the Python folder) as if you wanted to open it. Right click and select "show package contents". Open Contents, then open Resources. In Resources, you'll see a file called idlemain.py. This file executes when you launch idle and sets, among other things, the working directory. We're going to edit that.
But before you can edit it, you need to give yourself permission to write to it. To do that, right click on the idlemain.py and select get info. Scroll to the bottom of the getinfo window and you'll see the Sharing & Permissions section. On the bottom right there's a lock icom. Click the lock and follow the prompts to unlock it. Once it's unlocked, look to the left for the + (under the list of users with permissions). Click it. That will bring up a window with a list of users you can add. Select yourself (probably the name of your computer or your user account) and click Select. You'll see yourself added to the list of names with permissions. Click where is says "Read only" next to your name and change it to "Read & Write". Be careful not to change anything else. When you're done, click the lock again to lock the changes.
Now go back to idlemain.py and open it with any text editor (you could use Idle, TextEdit, or anything. Right under the import statement at the top is the code to change the default working directory. Read the comment if you like, then replace the single line of code under the comment with
os.chdir('path of your desired working directory')
首先找到文件。在 Finder 中,转到 Applications 中的 IDLE(在 Python 文件夹中),就像您想打开它一样。右键单击并选择“显示包内容”。打开内容,然后打开资源。在参考资料中,您将看到一个名为 idlemain.py 的文件。该文件在您启动 idle 时执行,并设置工作目录等。我们将对其进行编辑。
但在您可以编辑它之前,您需要授予自己写入权限。为此,右键单击 idlemain.py 并选择获取信息。滚动到 getinfo 窗口的底部,您将看到共享和权限部分。右下角有一个锁定图标。单击锁并按照提示将其解锁。解锁后,向左看 +(在具有权限的用户列表下)。点击它。这将打开一个窗口,其中包含您可以添加的用户列表。选择您自己(可能是您的计算机名称或您的用户帐户),然后单击“选择”。您会看到自己被添加到具有权限的姓名列表中。单击您的姓名旁边显示“只读”的位置,然后将其更改为“读取和写入”。小心不要更改任何其他内容。完成后,
现在返回 idlemain.py 并使用任何文本编辑器打开它(您可以使用 Idle、TextEdit 或任何其他工具。顶部的 import 语句正下方是更改默认工作目录的代码。如果您愿意,请阅读评论,然后将注释下的单行代码替换为
os.chdir('你想要的工作目录的路径')
Mine looks like this:
我的看起来像这样:
os.chdir('/Users/MyName/Documents/Python')
os.chdir('/Users/MyName/Documents/Python')
Save your changes (which should work because you gave yourself permission). Next time you start Idle, you should be in your desired working directory. You can check with the following commands:
import os
os.getcwd()
保存您的更改(这应该可以工作,因为您已授予自己权限)。下次启动 Idle 时,您应该位于所需的工作目录中。您可以使用以下命令进行检查:
导入操作系统
os.getcwd()
回答by Arnold
This ought to be the number one answer. I have been playing around this for an hour or more and nothing worked. Paul explains this perfectly. It's just like the PATH statement in Windows. I successfully imported a module by appending my personal "PythonModules" path/dir on my Mac (starting at "/users/etc") using a simple import xxxx command in Idle.
这应该是第一个答案。我已经玩了一个小时或更长时间,但没有任何效果。保罗完美地解释了这一点。它就像 Windows 中的 PATH 语句。我通过在 Idle 中使用简单的 import xxxx 命令在 Mac 上附加我的个人“PythonModules”路径/目录(从“/users/etc”开始)成功导入了一个模块。

