Auto Executable Python File Without Opening from Terminal

Auto executable python file without opening from terminal?

First, pick a file extension you want for files you want to have this behavior. pyw is probably a good choice.

Name your file that, and in your file browser associate that file type with python. In GNOME, you'd open its Properties window, go to the Open With tab, and enter python as a custom command.

Now here's the important part: That little dialog you've been getting asking you what you'd like to do with the file is because it is marked as executable. Remove the executable bit with chmod -x. Now when you double click it, it will simply be opened with the associated program.

Of course, if you want to run it from the command line, you'll now have to start it with python explicitly since it isn't marked executable. The shebang line doesn't matter anymore, but I'd leave it in anyway in case someone else marks it executable and expects it to work.

Run Python script without Windows console appearing

pythonw.exe will run the script without a command prompt. The problem is that the Python interpreter, Python.exe, is linked against the console subsystem to produce console output (since that's 90% of cases) -- pythonw.exe is instead linked against the GUI subsystem, and Windows will not create a console output window for it unless it asks for one.

This article discusses GUI programming with Python, and also alludes to pythonw.exe. It also helpfully points out that if your Python files end with .pyw instead of .py, the standard Windows installer will set up associations correctly and run your Python in pythonw.exe.

In your case it doesn't sound like a problem, but reliance upon pythonw.exe makes your application Windows-specific -- other solutions exist to accomplish this on, say, Mac OS X.

Python automatically run program without shell staying open

If you want to open or hide the window at your leisure, you have to use a GUI package such as TkInter. Then follow this tutorial on how to show or hide a window with TkInter.

If you run the script with pythonw.exe or you save the script as .pyw, the terminal window will be hidden.

You just need to open the job script using pythonw.exe.

Two lines in the command prompt to set all python files to open with it.
Python Documentation - 3.3.4 Executing scripts

  1. Launch a command prompt.
  2. Associate the correct file group with .py scripts:
    assoc .py=Python.File
  3. Redirect all Python files to the new executable:
    ftype Python.File=C:\Path\to\pythonw.exe "%1" %*

If you need to close the script but the window is hidden you can use the psutil module to find which executable is running your script, grab the PID, and kill it.

import psutil

scripts = [[" ".join(p.cmdline()), p.pid] for p in psutil.process_iter()
if p.name().lower() in ["python.exe", "pythonw.exe"]]

Python run process as .exe with out opening any console windows

Normally, python files have the extension .py . If you want to run your program without opening a console window, change the extension to .pyw.
To convert a .py file to a .exe file (with PyInstaller) which will not open the console, use one of the following commands (see here for more info):

pyinstaller -w yourfile.py

pyinstaller --windowed yourfile.py

pyinstaller --noconsole yourfile.py

How to keep a Python script output window open?

You have a few options:

  1. Run the program from an already-open terminal. Open a command prompt and type:

    python myscript.py

    For that to work you need the python executable in your path. Just check on how to edit environment variables on Windows, and add C:\PYTHON26 (or whatever directory you installed python to).

    When the program ends, it'll drop you back to the cmd prompt instead of closing the window.

  2. Add code to wait at the end of your script. For Python2, adding ...

    raw_input()

    ... at the end of the script makes it wait for the Enter key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts. For Python3, use input().

  3. Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.

Running an outside program (executable) in Python?

Those whitespaces can really be a bother. Try os.chdir('C:/Documents\ and\ Settings/') followed by relative paths for os.system, subprocess methods, or whatever...

If best-effort attempts to bypass the whitespaces-in-path hurdle keep failing, then my next best suggestion is to avoid having blanks in your crucial paths. Couldn't you make a blanks-less directory, copy the crucial .exe file there, and try that? Are those havoc-wrecking space absolutely essential to your well-being...?

How can I make a Python script standalone executable to run without ANY dependency?

You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.

It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).

How to make a program convert a .PY file to .EXE without human intervention and without GUI

I'll be using the cx_Freeze library to convert .py to .exe.
To install the package enter the command python -m pip install pypiwin32 in the terminal.

import win32com.client

tempFile = open(os.path.abspath("temp\\temp.py"), 'w')
your_File = 'yourFile.py'
print("import sys\nfrom cx_Freeze import Executable, setup\n\n\nbase = None\nif sys.platform == \"win32\":\n base = \"Win32GUI\"\nexecutables = [Executable(r\"" + yourFile + "\", base = base)]\n\nsetup(\n name=\"exe_File\",\n version=\"0.1\",\n description=\"Sample cx_Freeze script\",\n executables=executables,\n)", file = tempFile)
tempFile.close()

os.system("python \"temp\\temp.py\" build")


Related Topics



Leave a reply



Submit