Search code examples
pythonpython-3.xcolorama

Program to run another through command prompt


I am not even sure if this question is answerable. Basically in my game I am using the colorama features to make it look nice, but the colorama features only work when you access python in command prompt, so my question is how I can get python program to run another via command prompt, is this doable or not? I have tried installing win32 but that is in python 2 format and I am using 3.4 so I was getting syntax errors that I wasnt sure how to fix.


Solution

  • I am not sure why is this happening, I mean, colorama not working without starting from prompt.

    Perhaps something with environment variables PATH or something.

    This is one suggestion, and I am not sure that it will work as we will not be changing the window program is running in, just invoking cmd.exe i.e. command prompt to start within it and start python and your script again.

    But it is worth a try:

    # Start of your program:
    import sys, os
    
    if "started_with_prompt" not in sys.argv:
        cmd = 'cmd /C "'+sys.executable+' '+" ".join(sys.argv)+' started_with_prompt"'
        os.system(cmd)
        sys.exit()
    print "the rest of your program"
    

    If this doesn't work, there are tricks that can be used through subprocess module to do the similar thing. Also, you should look at cmd.exe's help to see whether you should use some other switch than /C to enable environment and/or registry extensions.

    But, essentially, you should be able to get the same result by making a shortcut with the command like one in cmd variable, or a batch file that starts Python. Like this:

    @echo off
    cmd /C "C:\Python27\python.exe path_to_your_script.py"
    

    I think both would work, but somehow that you wouldn't like this solution.

    Well, I think that shortcut would need a full path to cmd.exe which is: C:\Windows\system32\cmd.exe

    Let me know if it doesn't work.