Clickable Python CLI Instantiation - python

I'm trying to make my .py script clickable!
What I Have
The CLI program I created using Python works. I open Command Prompt and enter python my_script.py and everything works as expected. I want to make it clickable so a user doesn't have to run any commands!
What I've Tried
I created a .bat file in vim that runs the python my_script.py command. When I click it, the desired behavior is not achieved. Command Prompt opens for about a millisecond then closes.
My Question
How can I create a better .bat file so that my CLI program works with the click of a button?

Python, for better or worse does not allow you to specify the path to the script, and will not search the path for the script.
The official word, and I hate to say this, is that you must associate the Python.exe executable you want to run python scripts with the extension .py and then you can double click your script.py or type \path\to\script.py into the CMD prompt, and it will run the script.
I hate this from a standpoint of not wanting to accidentally execute a script by clicking it, and from the perspective of wanting to have multiple versions of python installed and choosing which one would do the execution.
There is an (IMHO) annoying (Kludgy) work-around.
You can create a File type for each instance of Python, you might have, and then associate it with the .py
so you could create the FTypes you need and associate them in a script at the time of to launch the script from the batch file:
FTYPE Py23=C:\Path\To\Pthon-23\python.exe %*
FTYPE Py38=C:\Path\To\Pthon-38\python.exe %*
Then your script could do this:
#Echo off
ASSOC .py=Py38
"C:\Path\To\Script.py"
ASSOC .py=

Python supports being called in a mode where it can ignore the first line of the script with -x. This coupled with a wrapper batch file means you can actually put the entire Python script in a batch file.
This little script shows how this can be done. It's a self-contained
#echo off
rem = """ # This is run in the batch file to launch Python
python -x "%~f0" %*
goto endofPython
rem = """
# Your python code goes here ..
def main():
print("Hello from Python")
if __name__ == "__main__":
main()
rem = """ # This section is run after Python is done, here just
# just run pause to keep the console window open
:endofPython
pause
rem = """

Related

Get current path when running Python from Notepad++

I run Python scripts in Notepad++ using this command
cmd.exe /K "C:\InstallPython\python.exe" "$(FULL_CURRENT_PATH)"
it works, but it works not great.
When I run
exec(open("raw_ticker_list.lua").read())
it doesn't see the file, but it is in the same folder where the script lies.
When I run
import os
print(os.getcwd())
it prints
How can I make python see files in current folder?
Use this command instead:
cmd.exe /K "cd /D "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""
To recap, open the "Run" menu, select the "Run" entry, and enter the above command as "The Program to Run". Possibly "Saveā€¦" it and assign a name (and keyboard shortcut) so that it permanently appears in the "Run" menu going forward.
What it does is, it opens a command window, changes the working directory to that of the script currently active in the editor (across hard drives, hence the /D parameter), then runs the Python interpreter on the script, but keeps the command window open afterwards (the /K parameter).
Use the full path to python.exe instead of just python in case it's not on the Windows search path for executables.

Batch file is not working

#echo off
start c:\Python27\python.exe C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py
PAUSE
My script in tool.py is correctly working in PyCharm IDE, this bat is not working.
Note : file path and python path is correct.
Any other option to run python script independently
Try removing the start from your batch file - this opens a separate window for the console application python.exe, and when python.exe exits, it will close the console window. By eliminating start, it runs python.exe in the same session of cmd that the batch file is running in, and allows you to read any output from python.exe before exiting (because of the pause command).
Python installations by default associate .py files with it and issues a command to run the interpreter PATH_TO_PYTHON\python.exe "%1" %*
So you should not need to call python.exe manually to call the script. You also do not need the start command at all.
Not sure why you need the batch to call a python script, but this would be perfect. always put the full path in quotes in case you hit whitespace in the paths.
#echo off
"C:\Users\anupam.soni\Desktop\WIND_ACTUAL\tool.py"
pause

How to execute a Nautilus script written in Python inside a gnome-terminal window that stays open?

Lets say I want to execute a simple Python script from Nautilus, the default file manager of GNOME:
#!/usr/bin/python3
print("Hello")
Of course the aim is to interact with selected files in Nautilus, but I want to keep it simple.
I save the script to the folder ~/.local/share/nautilus/scripts/ and then I can execute it from the right click context menu:
How can I execute this nautilus script inside gnome-terminal and keep the terminal opened at the end of the script?
I have found that I can achieve what I want to do using two script files.
1) Hello.sh to open gnome-terminal (and potentially leave it open)
The first script file ~/.local/share/nautilus/scripts/Hello.sh will appear in Nautilus script context menu and will open gnome-terminal in order to execute .Hello.py:
#!/bin/bash
gnome-terminal -- python3 ~/.local/share/nautilus/scripts/.Hello.py
To force the terminal window to stay open after execution (to see output or for debugging purpose if it fails), tweak it as follow to make gnome-terminal execute bash at the end:
#!/bin/bash
gnome-terminal -- bash -c "python3 ~/.local/share/nautilus/scripts/.Hello.py; bash"
2) .Hello.py to execute the actual script
Then, the second script file ~/.local/share/nautilus/scripts/.Hello.py will be executed inside the gnome-terminal windows opened previously but will be hidden from nautilus script context menu.
#!/usr/bin/python3
print("Hello")

Is there a way to shorten command line commands to open Python scripts?

I'd like to know if there is a way to shorten what must be written on the cmd.exe command line to run Python programs. As it is, I have a program called Calculator.py and if I wanted to run that I would have to write:
python.exe C:\Users\user_name\restofdirectory\Calculator.py
Basically I want to know if I could make the 'Python' folder on my computer default so I could just type python.exe Calculator.py or some other similarly short way of doing it so I didn't have to type the whole directory.
Create a cmd.exe shortcut somewhere handy (Desktop or whatever). Right Click, select properties. Under the Shortcut tab you'll have a "Start In" field. It'll be default to wherever your cmd.exe normally opens. But you can change it to any path you want (So set it to your python development folder)-- then opening that shortcut will always start cmd.exe in the python folder.
You don't need to type python.exe file.py --
Just type: python file.py
So in short, you'd click that shortcut and type python file.py and you are done.
If you have specific scripts you want to run frequently, you can create a cmd.exe shortcut for each one. Set the Start In path to their folder. Then update the link to cmd.exe like this:
c:\path\to\cmd.exe /k python file.py
That will open a cmd prompt and automatically run that specific script
Yes, you can cd into the folder first.
If python is in your default path you can create a folder and put all your python scripts in it. Then cd into that folder and type python Calculator.py. To test if python is in your path simply open up a cmd and type cd \ (which in your case should change your working directory to C:\) then type python. If you get an error saying the command python cannot be found then you have to add python to your path. Other wise you should be able to run cd C:\Users\user_name\restofdirectory\ and then execute the script python Calculator.py
create an environment variable, say, PyPath = C:\Users\user_name\restofdirectory
then your line would be:
python.exe %PyPath%\Calculator.py
or you just cd to C:\Users\user_name\restofdirectory, and run
python.exe Calculator.py
Add Python to your path:
SET PATH=%PATH%;C:\Python27\bin
then you can call python like this:
python myapp.py
If you have the Python Launcher for Windows (comes with Python 3.3+) installed, then it gets even easier. You just need to put shebang in your code:
#! python
The first time you run your code, Windows will pop up a dialog and ask you what program to use to run the script. The Python Launcher should be listed. Just choose that and away you go. Here's a fun little script from the page I linked earlier:
#! python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
Once my Windows 7 box had the launcher installed and I told it what program to run, I could just call my script from cmd.exe like this:
my_script.py
Note: If you have a version of Python older than 3.3, you can download the installer here

Run python script in terminal

I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.

Categories