starting Python IDLE from command line to edit scr

2019-02-22 02:09发布

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

7条回答
对你真心纯属浪费
2楼-- · 2019-02-22 02:38

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!

查看更多
对你真心纯属浪费
3楼-- · 2019-02-22 02:40

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.

查看更多
我只想做你的唯一
4楼-- · 2019-02-22 02:44

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.

查看更多
贪生不怕死
5楼-- · 2019-02-22 02:53

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.

查看更多
ら.Afraid
6楼-- · 2019-02-22 03:01

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

查看更多
混吃等死
7楼-- · 2019-02-22 03:01

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

查看更多
登录 后发表回答