Default working directory for Python IDLE?

丶灬走出姿态 提交于 2019-11-30 13:37:14
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.

1) Right-click "Properties".

2) Select "Shortcut" tab.

3) In "Start In", write file path (e.g. "C:\Users...").

This is also my answer here: Default save path for Python IDLE? Let me know if this works!

I've found a solution after looking into PyShell.py:

  1. Create a python file under a preferred directory, in my case '~/.idlerc/init.py', and copy/paste the following lines:
  import os
  os.chdir('<your preferred directory>')
  1. 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):
  /usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py

Just use a shell script such as:

#!/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.

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

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.)

Example 2.4. Import Search Path

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 ....

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.

One default path is specified in idlelib.IOBinding.IOBinding.dirname or idlelib.IOBinding.IOBinding.filename

Ubuntu

So my idle-python3.desktop file in /usr/share/applications looks like this:

[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.

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.

kalle123

All I had to do here (Linux Mint 18.2 Xfce) ...

Just add path in line "working directory" = "Arbeitsverzeichnis"

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.

The idea is to edit a resource file inside of the IDLE package in Applications.

  1. 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.

  2. 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.

  3. 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')

Mine looks like this:

os.chdir('/Users/MyName/Documents/Python')

  1. 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()

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!