10

I've tried many variations of this command: idle.py -e filepath, but it simply starts IDLE like normal, not opening any extra windows for editing, and not throwing any errors.

So how can I do the equivalent of opening IDLE, file>open>filepath via the command line (or perhaps even a Python module)?

piet.t
  • 11,718
  • 21
  • 43
  • 52
Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62

8 Answers8

17

You need to do as stated in the main.py file of the idelib folder (C:\Python33\Lib\idlelib), at least on the python 3.3 version explains that:

IDLE main entry point

Run IDLE as python -m idlelib

So with python -m idlelib <script_to_edit> you will be able to open and edit the script with idle. I haven't checked with previous versions but it could be the same comand

This is also documented on the changelog of the version 3.3.3

llrs
  • 3,308
  • 35
  • 68
  • In a Windows shortcut, this becomes `C:\Python34\python.exe -m idlelib "path/to/script.py"`. It launches a command window behind IDLE, but that goes away as soon as you close IDLE. – Noumenon Oct 02 '16 at 01:25
  • 1
    This works perfectly fine in my Windows 10 machine. I used `py` though: `py -3 -m idlelib myscript.py` – HelloWorld101 Jan 10 '17 at 13:39
  • You can use pythonw.exe (note extra 'w' in name) on Windows to avoid the command window showing behind IDLE. – Daz Jun 11 '18 at 12:31
6

Make a new text file, and put something like this in it:

C:\Python26\Lib\idlelib\idle.pyw "C:\file1.py" "C:\file2.py"

In your actual script, you'll replace "C:\file1.py" and "C:\file2.py" with your files' paths, save as a .bat, and then launch it. That should do what you want.

koyae
  • 720
  • 7
  • 12
Redouane Zait
  • 175
  • 3
  • 8
  • That doesn't work for me. It has the same results as my example. – Ponkadoodle Feb 27 '10 at 00:39
  • 1
    Perhaps there is something wrong with my version of IDLE. As a test, I tried `idle -h`, which according PyShell.py should print the help. It only opened the shell though, no output at all. Which version of Python are you using? (2.6 for me (edit: looks like you are too)) – Ponkadoodle Feb 27 '10 at 01:25
  • I found a workaround: `python "C:\Python26\Lib\idlelib\idle.py" "C:\file1.py"` So something with the argument passing simply wasn't working. Not sure what, but hey, it works! – Ponkadoodle Feb 27 '10 at 01:35
  • 1
    `idle.pyw` didnt work for me, but the same folder has a `idle.bat` :) – Mike R Apr 10 '14 at 21:40
  • If running `idle.pyw` directly doesn't work, then probably the file association is wrong. Make sure that `assoc .pyw` is set to `Python.NoConFile` and that the command for `ftype Python.NoConFile` has the args `"%1" %*`. Prior to 3.3 the default command runs `pythonw.exe` (make sure it's the right version), and for 3.3+ it runs `pyw.exe`. – Eryk Sun Jun 09 '14 at 18:38
  • With `py` and `pyw` installed, it's convenient to run `py -X[.Y] -m idlelib [args]` to run IDLE with an attached console or `pyw -X[.Y] -m idlelib [args]` to run without an attached console. See [Python Launcher for Windows](https://docs.python.org/3/using/windows.html#python-launcher-for-windows) in the docs. – Eryk Sun Jun 09 '14 at 18:43
  • In my case I had to use the -r switch I found elsewhere `C:\Python27\Lib\idlelib\idle.bat -r "C:\temp\module1.py" ` – Dowlers Apr 29 '15 at 22:46
  • This is beautiful! If you ignore adding the "C:\file1.py" etc, and just use the first part, it launches idle! – VISQL Jan 20 '16 at 06:20
5

Please forgive me for bumping such an old thread, but I've been teaching myself linux and python with the help of the community, and was trying to figure out how to invoke IDLE2 and IDLE3 from the command line. I came across this post some of the solutions seemed a bit complicated for my application. Then it occurred to me that I could just put syslinks in the /usr/bin/ path for each.

sudo ln -s idle-python3.1 idle3
sudo ln -s idle-python2.6 idle2

to address the OP. From the directory the script is located, type:

idle3 abc123.py 

or

idle2 abc123.py

I'm just so damned happy that I finally had a "light bulb" go off that I wasn't going to let a 2 year old post stop me from posting my solution.

GhostRyder
  • 51
  • 1
  • 3
  • 4
    Don't feel bad for reviving old threads (especially if they're unanswered!). StackOverflow exists to provide answers for those who ask, like myself, but also to serve as a reference for readers like yourself who are searching for solutions to similar problems. So thread revival isn't discouraged here. Thanks for your answer :) – Ponkadoodle Nov 26 '12 at 06:14
3

Rarely the native os is useful. I created a 'win batch file, in the folder with my .py files:

start /MIN cmd /C c:\Python27\lib\idlelib\idle.py -e %1 %2 %3 %4 %5 %6

This can open up to six files from cmd line in one shot. Just type the name of the batch file, followed by from zero to six filenames. Also if one or more files you specify are not found, idle opens these as new document(s).

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
  • 3
    Note: In windows you can replace %1 %2 %3 ... with simply %* – kodybrown Sep 25 '12 at 17:16
  • You can add `.LNK` to `PATHEXT` to run a shortcut as a command; leave the working directory empty if you want it to inherit the parent's working directory. It's easier to manage than using a batch file to emulate a shortcut, and doesn't have the contortions of running cmd.exe to run a batch file to use the start command to run cmd.exe to run the actual command. – Eryk Sun Jun 09 '14 at 18:51
2

first make sure you have location of idle in path
I am using "python3.5".So mine looks like this: C:\Program Files\Python35\Lib\idlelib.Yours may differ.
use this following command:idle -r file_name.py to run the file or just idle file_name.py to edit


or start idle -r file_name.py ^&exit
0

you can just program in Python to edit your Python files. A simple example. say you want to search for a word and then replace it with something else.

import fileinput
import os
os.chdir( os.path.join("c:\\","path") )
for file in os.listdir("."):
    for line in fileinput.input(file,inplace=0):
       if "search word" in line :
           line=line.replace("search word","new word")
           print line

(use inplace=1 to do in place editing.). Then save and run the script as normal Python script using the interpreter.

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • why do you think so? you save it as a normal python script, and run it on the command line with the interpreter. Its the same as writing a batch file, instead you are using Python, not batch or vbscript. Its better than opening IDLE and editing by hand one by one. – ghostdog74 Feb 27 '10 at 01:11
  • Well I mean that you'd have to know the exact phrase you want to replace, without being able to actually browse the contents of the file. Perhaps I misunderstood what this does, but it looks like it takes a file and replaces all "search word"s with "new word"s. Or perhaps you misunderstood my question: I want to automate the opening of 15 files for editing. – Ponkadoodle Feb 27 '10 at 01:22
  • i see. i misunderstood indeed. – ghostdog74 Feb 27 '10 at 01:48
0

Just add IDLE's path to your PATH environment variable.

For example I created an environment variable called IDLE_PATH and set the value to C:\Python27\Lib\idlelib

Then in my PATH variable I added ;%IDLE_PATH%; and open a new cmd prompt or in console2 just open a new tab and run idle <file_name> to open the file, you will be able to do this from any directory. In IPython console add an ! before the command, for example !idle test.py.

Congrates, Now you're a python pimp!

0

paste the idlelib to your system path or user path, environment variable.for example, like this C:\Program Files\Python310\Lib\idlelib

then type idle in your command prompt. done.

Ab C
  • 1