Search code examples
python-3.xcmdsublimetext3

How to run a python file/script from sublime text 3 on cmd in windows 10?


I have been searching the internet up and down for a solution to this. I understand there is SublimeREPL which I can easily use to properly run python code in sublime text 3. However, it's not sufficient for me. I want to run my python file on the cmd from sublime text 3.

There are many reasons for me. The biggest reason is that i need to test my python files often and I don't want to cramp my sublime text workspace with thousands of tabs. There are several other reasons but at the moment I just want a solution. Almost everywhere on the internet it only talks about SublimeREPL, so I have been unable to find a solution.

I also understand I can use the cmd manually by going to the file directly and running it there, but its a pain in the back to keep switching to cmd and then to sublime text over and over again.

Therefore, I am looking for a neat solution where I can run my python file from sublime text 3 in cmd. Any help is deeply appreciated.

I am using python 3.7.3


Solution

  • The rule of thumb for build systems in Sublime Text is that if you can craft a command line that, when executed manually in the terminal/console, will give you the effect that you want (and that command doesn't require interactive input), then you can turn it into a build system.

    In this case, what you want to do is spawn a new cmd window and do something inside of it; the fact that you're using Sublime is thus not interesting in the grand scheme of knowing how to do that, which might be why your search didn't turn up any results.

    In Windows, you can use a terminal command like cmd /s /c something to tell the Windows cmd.exe that it should execute the command something. In your case you want to use Python to execute a program, so that might look something like the following to get Python to execute my_file.py.

    cmd /s /c python my_file.py
    

    However if you do that in an existing command prompt, the result is just to run the program in the current window; i.e. you're telling cmd to run a command, but it's still running in the current window, which is not what you want.

    In order to run the program in a new window, you need to use the start command, which launches a new program. The general format of that would be something like this:

    start "" cmd /s /c python my_file.py
    

    That tells start to launch a new instance of cmd, which you're telling to run the program, so now the Python program is running in its own window.

    There are some general issues with this; the biggest one is that as soon as cmd finishes executing the command you give it, it quits. Similarly, Python also quits when the program is finished. The result of that is that as soon as your program is finished, the window immediately vanishes before you can see what it did.

    You could add -i to the python command to get it to go interactive, but then you'd have to interact with the internal python REPL in order to get it to quit and close the window, which it sounds like you don't want to have to take the step to do.

    In that case you need to modify the command you give to cmd to get it to also wait until you press a key.

    All told, an example of that might look something like the following as a complete sublime-build file:

    {
        "shell_cmd": "start \"\" cmd /s /c \"python -u \"$file\" & pause\"",
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
    
        "env": {"PYTHONIOENCODING": "utf-8"},
    }
    

    This is a version of the internal Python.sublime-build modified to extend the command as outlined above. In order for this to work, you need to be able to enter python at a terminal and have it launch the Python interpreter. If that's not the case you need to adjust your PATH environment variable as appropriate.

    If you're using Python 3, you may need to replace python with python3 or similar in the command so that cmd knows what to execute.