Default working directory for Python IDLE?
Asked Answered
S

9

11

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.

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.

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.

Stilliform answered 12/3, 2013 at 17:11 Comment(2)
This works for the default save path as well.Stilliform
possible duplicate: #1424898Leonidaleonidas
H
10

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!

Heng answered 11/12, 2013 at 1:51 Comment(1)
It seems Windows 10 has abolished the traditional right-click Properties menu so you need to find the shortcut - most likely in C:\ProgramData\Microsoft\Windows\Start Menu\ProgramsRecuperative
S
5

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
Stilliform answered 12/3, 2013 at 17:27 Comment(1)
This did not work on my Ubuntu system until I expanded the home directory '~' shortcut to the full path to init.py, e.g., /home/<username>/.idlerc/init.py. (Replace <username> with your own home directory name.)Panama
P
2

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.

Pondweed answered 12/3, 2013 at 18:56 Comment(1)
Works perfectly on Ubuntu Linux, too. I modified the entry in my GNOME main menu to call the shell script instead of launching Idle directly.Panama
C
1

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

Cho answered 1/4, 2016 at 11:10 Comment(1)
this looks like the official way to do it!Cho
B
1

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

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

Bondon answered 10/1, 2018 at 19:33 Comment(0)
C
0

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.

Coelostat answered 12/3, 2013 at 17:14 Comment(1)
Thanks. But as I said I knew it defaulted to my home directory, which was not what I wanted. The question was about starting IDLE to my preferred directory. I've just found an answer and will post it shortly.Stilliform
L
0

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.

Leonidaleonidas answered 16/5, 2015 at 6:31 Comment(0)
F
0

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

Fedora answered 2/3, 2018 at 14:45 Comment(0)
B
-1

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.

Brott answered 22/1, 2019 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.