Getting user input while running a python script i

2019-04-06 13:39发布

问题:

Not really sure whether I'm just not looking in the right place or whether this feature is yet to be implemented, but after installing the atom script package and testing it out on a program that requires user input, I realize that I can't type in anything for input() the way I can when running the program from the shell. I stumbled upon this thread which makes me suspect that the feature hasn't been added, but I just wanted to be sure. Isn't this a pretty basic thing to be able to do? Or do I have to stick to using atom purely as a text editor and running the file from the CLI?

回答1:

Some text-editors (including Atom and Sublime) don't like user input (raw_input()). Yes, you'd have to run the file from CLI.

You could, however, get around this problem by using other text editors like Notepad++ (see this answer to run Python in notepad++ - How to Execute a Python File in Notepad ++?), where user input works fine.

If you prefer to switch to Sublime (which also has a problem with user inputs), see this answer - Sublime Text 2 console input.

If you'd want to stick with Atom, an alternative, of course, would be to hard-code the variables you are looking for in raw_input while debugging/developing (but don't forget to switch back to raw_input after debugging).



回答2:

Install atom-shell-commands .
Look up at the Running in a new window sample in the linked page.
Edit the config file like this :

"atom-shell-commands":
    commands: [
      {
        name: "run with python 3"
        command: "cmd"
        arguments: [
          "/C"
          "start"
          "$your_folder$/launch_python3.cmd"
          "{FileName}"
        ]
        options:
          cwd: "{FileDir}"
          keymap: 'ctrl-3'
      }
    ]

Note : I saved the launch_python3.cmd in my user folder /.atom, but you can save it elsewhere, it should not be an issue.

The cmd file contents :

@echo off  
REM used by atom-shell-commands to launch python 3 in a new window

$your_python_path$\python.exe %1  
pause  
exit 

Now, you'll find a 'run with python 3' under Packages > Atom Shell Commands.
Edit the name and the keyboard shortcut as you see fit.
Clicking on the menu, a new command prompt window is displayed : it supports also user input.
Worked for me.