Windows: run python command from clickable icon - python

I have a python script I run using Cygwin and I'd like to create a clickable icon on the windows desktop that could run this script without opening Cygwin and entering in the commands by hand. how can I do this?

The simplest solution will be creating batch file containing command like:
c:\python27\python.exe c:\somescript.py
With this solution you will have to have installed python interpreter. If you need more portable solution you can try for e.g. py2exe and bundle python scripts into executable file, able to run without requiring a Python installation.

This comes straight from Python Docs (https://docs.python.org/3.3/using/windows.html):
3.3.5. Executing scripts without the Python launcher
Without the Python launcher installed, Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*

This tutorial shows how to create a batch file that runs a python script.
Note that many of the other answers are out of date - py2exe is out of support past python 3.4. More info here.

As an alternative to py2exe you could use the pyinstaller package with the onefile flag. This is a solution which works for python 3.x.
install pyinstaller via pip
Package your file to a single exe with the onefile flag
pyinstaller --onefile your_file.py

A better way (in my opinion):
Create a shortcut:
Set the target to
%systemroot%\System32\cmd.exe /c "python C:\Users\MyUsername\Documents\MyScript.py"
Start In:
C:\Users\MyUsername\Documents\
Obviously change the path to the location of your script. May need to add escaped quotes if there is a space in it.

The solution that worked like a charm for me >
From https://www.tutorialexample.com/convert-python-script-to-exe-using-auto-py-to-exe-library-python-tutorial/
pip install auto-py-to-exe
The GUI is available just by typing:
auto-py-to-exe
Then, I used this command to generate the desired output:
pyinstaller --noconfirm --onedir --windowed --icon "path/favicon.ico" "path/your_python_script"
Now I have my script as executable on taskbar

Related

How do i make my program recognized by Windows Protector and AntiViruses?

I am trying to make a program, but when I turn it into .exe it's unrecognized by windows protector. And when I tried to run it in virtual box (virtual pc) AVG Antivirus detects it as a rare file, and it deleted it when I ran the program, how do I make it secure? I used "auto py to exe" to turn python codes to exe.
Auto py to exe uses pyinstaller to convert it, It uses GUI so it's much easier to use. It also shows the command of the pyinstaller.
Probably your python to exe convertor has bad reputation. Try a popular one pyinstaller, using next steps:
Install pyinstaller through python -m pip install pyinstaller.
Package your python script e.g. example.py through pyinstaller --onefile --noupx example.py, result will be in file dist/example.exe.

How to run pyinstaller from python script already converted to exe?

I'm trying to run pyinstaller in python exe file in order to someone without python can use pyinstaller but no idea how to do it.
I tried import PyInstaller with other needed modules and convert this script to exe but I got error "The 'PyInstaller' distribution was not found and is required by the application". I also tried to pack PyInstaller exe file but didn't worked too. Python 3.6.5
Any ideas how to do it?
Unfortunately, what you're describing is not possible with PyInstaller. I submitted an issue on GitHub, and this is what one of the developers said:
Nope, this won't work.
PyInstaller internally uses a lot of sub-processes where it is assumed that sys.executable points to a python interpreter (e.g., here); this is not true in a frozen application, where it points to the frozen executable, which ends up in effectively endless recursion.
Even if this was not a problem, the frozen application includes only a subset of python environment, so it cannot be used to freeze an arbitrary script (not to mention the issue of 3rd party packages).
So whatever use case you have for this, it cannot be supported by PyInstaller.
check the requirements for the Pyinstaller from this link initially
https://pythonhosted.org/PyInstaller/requirements.html
Then install Pyinstaller by,
pip install pyinstaller
To create .exe file use this command for basically,
pyinstaller your_script.py
To run the created .exe
METHOD 1
Do double click the .exe file in your directory.
METHOD 2
In your cmd prompt load in to your current directory or project directory then do
: .\dist\your_script.exe
because the create .exe files are saved in dist folder inside to the project folder with the name of your script file names only.

Python to EXE file in one file

So far I have used cx_freeze to convert .py file to .exe file, but I get many files. Is there a way to get it all into one executable?
I have seen that PyInstallerGUI is able to that, but it is for Python 2.7. Can it be done with Python 3.4 as well?
PyInstaller works up to Python 3.5. Once you've installed it (type in your terminal pip install pyinstaller), you can do in your terminal:
pyinstaller --onefile script.py
where script.py is the name of script you want to compile into .exe
With the --onefile option it will create only one .exe file.
I haven't tried it but, PyInstaller says here it can do that and it supports Python 2.7 and Python 3.3+.
Quoting from the linked page:
PyInstaller can bundle your script and all its dependencies into a
single executable named myscript (myscript.exe in Windows).
The advantage is that your users get something they understand, a
single executable to launch. A disadvantage is that any related files
such as a README must be distributed separately. Also, the single
executable is a little slower to start up than the one-folder bundle.
Before you attempt to bundle to one file, make sure your app works
correctly when bundled to one folder. It is is much easier to diagnose
problems in one-folder mode.
I found this in the PyInstaller documentation:
pyinstaller --onefile your-python-file.py
To find more: PyInstaller documentation

Packaging a Python script that can be run on a computer that doesn't have Python installed?

I have a Python script (that manipulates files on a shared drive). I would like to turn that script into a file (maybe .exe?) that perhaps can be double-clicked and run on a machine that doesn't have Python installed. Is that possible/easy to do?
You can try py2exe.
Tutorials can be found at http://www.py2exe.org/index.cgi/Tutorial
I use and recommend pyinstaller. If you need only one executable file, you need to install pyinstaller and call executable passing these arguments:
pyinstaller.exe your_script.py --onefile --windowed
This options can be used when you generate the executable file: https://pythonhosted.org/PyInstaller/usage.html#options
To install: http://www.pyinstaller.org/
Documentation: https://pythonhosted.org/PyInstaller/

Configuring Pycharm to run Pyinstaller

Yes I want to create a run configuration in PyCharm to run Pyinstaller and get my executable. According to the Pyinstaller documentation you should be able to locate an python script called pyinstaller-folder/pyinstaller.py after the installation, but it wasn't there. Then I look carefully and found this other one named pyinstaller-folder/__main__.py which should be the same <--(me wild guessing), so I set up my running configuration like this:
After running it, is giving me this error:
/usr/local/Cellar/python3/3.4.3/bin/python3.4 /usr/local/lib/python3.4/sit
e-packages/PyInstaller/__main__.py --onefile --nowindow --osx-bundle-identifier=jg.optimizer -F --name=genoptimizer optimizer/manage.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/PyInstaller/__main__.py", line 26, in <module>
from . import __version__
SystemError: Parent module '' not loaded, cannot perform relative import
Process finished with exit code 1
It seems to require a parent module to run but, how would that look like?
After more than two years, perhaps there is a better option.
In the PyCharm menu go to File -> Settings.
In the Settings dialog find Tools -> External tools and use the green + to add a new external tool.
For example:
Then, the IDE will allow you to run it on any python script. Right click on the file and the context menu will show External tools -> PyInstaller.
The PyInstaller package is a runnable module and can be run using python -m PyInstaller. To configure it as a run target in PyCharm, leave the "Script" field blank, write -m PyInstaller in the "Interpreter Options" field, and put the PyInstaller parameters into the "Script Parameters" field.
For example:
PyCharm will complain that the "Script" field is empty, but it will let you run the configuration anyway.
The run command has changed a bit since the accepted answer. You can now select a module to launch rather than editing the interpreter options.
As of writing this answer, here is how it looks like:
Notes:
This solution requires to install PyInstaller in the virtual environment of the project.
I am using PyCharm pro 2020.1
Old solution should still work
Using external tool is still a possibility. Which solution you choose is a matter of personal preference.
Install pyinstaller in pycharm, then open the pycharm terminal and write python -m PyInstaller.
So as Pycharm has newly updates, my case was a bit different as I installed pyinstaller from the interpreter settings as the following picture shows:
For Linux Users:
You could install it in both Python 2.7 or Python 3.7+. Make sure to get the path of where pyinstaller was stored.Then in the Settings option, try to find Tools -> External tools and add a new external tool as the following picture shows:
For Windows users:
If you are using Pycharm or any virtual environment. Unfortunatelly Pycharm creates its local vertual environment in venvpath once you indicate the interpreter. So, you should set the external tool (pyinstaller) to the real path of your python 3.7 .exe as the picture shows
For those of us on Windows with Anaconda trying to figure this out, I found it easiest to just set up a Bash Configuration (I believe you need the BashSupport plugin for this), and set:
Script: pyinstaller (assuming pyinstaller is in your path, if not, the full path)
Interpreter path: C:\Windows\system32\cmd.exe (yes, a bash configuration can just use the standard command program)
Interpreter options: /C
Program arguments: script_name.py (along with any other pyinstaller arguments)
Working Directory: C:\Path\To\Script

Categories

Resources