I want to start a python script using the Windows Explorer's right-click context menu.
These are my registry entries to achieve that:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\wescript.py\" %1"
I also edited the environment variables so that if I simply run the command:
"C:\Users\Admin\PycharmProjects\clean_desktop\wescript.py" "some_argument" in cmd it works just fine.
I tested it with a simple .exe file compiled from an c program
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\ctest.exe\" %1"
And it works just fine. But if I try the same with a .py file Windows states that: This Application cannot be opened on this PC
Is there a way to achieve what I want without the need of making an .exe file out of the .py file ?
Related
I use PythonWin as my main script editor.
To differentiate between scripts I use different extensions, *.py for Python 2 scripts and *.py3 for Python 3 scripts.
I have each extension opening in its appropriate version of PythonWin.exe through the HKEY_CLASSES_ROOT registry entries, so that's fine.
The problem is whenever I have PythonWin.exe version 2 running and I click on a *.py3 file it opens my script in the currently open version 2 version of PythonWin. Similarly if I have version 3 open and I open a *.py file it opens in the currently open version 3 PythonWin.
I am wondering if anyone else has had this issue and whether they have found any kind of descent work-around so each file extension opens in their respective version of PythonWin.
It turns out PythonWin.exe has some passable arguments to allow it to open more than one instance.
You can force it to open a new process by using the command
"C:\python27\Lib\site-packages\pythonwin\Pythonwin.exe" /new "%L"
Using this as the opening command in the registry entries for .py and .py3 would open each in their respective version of PythonWin.
The only disadvantage is that each time you open a new file, it will open a new PythonWin instance.
/new will also disable any Dynamic Data Exchange(Windows uses this to communicate between open applications) so the instance of PythonWin will never open any new files opened from Windows Explorer within it.
Once you have an instance open, you may as well just use the "Open" filemenu option to open new files, or just have multiple instances of each version of PythonWin open.
The thing in pythonwin.exe that looks for an existing instance and opens a *.py file in that instance is a protocol called a DDE(Dynamic Data Exchange) server. PythonWin opens one up every time it runs and when it detects a DDE server on the same channel, it passes the previous instance some python code to open the file and then terminates.
The easiest way to prevent PythonWin 2.7 from talking to PythonWin 3.9 is to have them open separately named channels to talk on.
Change the channel name in the intpyapp.py file.
Look for these two functions
def MakeExistingDDEConnection(self):
and
def InitDDE(self):
Change the channel name from "Pythonwin" to "Pythonwin37"(or something) IN BOTH FUNCTIONS
Delete any "intpyapp.pyc" files from your computer and try it out! You'll love it.
(*.pyc files store a compiled version of the code in the *.py file so no changes will be made to the running code until these are deleted)
If that still doesn't work, check out what command line is being used to run pythonwin.exe in the Windows registry
\HKEY_CLASSES_ROOT\Python.File\shell\open\command
and make sure there's no /new or /nodde parameters in the command line.
Here's an example of a good registry entry
"C:\python37\Lib\site-packages\pythonwin\Pythonwin.exe" "%L"
I am practicing python code and executing it through command line. each time i have to execute my python file i go to cmd and write
D:\PythonPractice>python myClass.py
Is there any standard and comfortable way available to execute python code quickly?
If you are executing the same command, with no changes to arguments or anything, you can pack it in a .bat file (windows executable)
Name it something like myscript.bat
D:\PythonPractice>python C:\path\to\myClass.py
Put full path inside it. Now double click will do.
Use PyCharm Software by JetBrains (same company who developed Android studio and Kotlin language), it will help you in many ways .
Run Python with single press of a button.
Add modules easily just with some clicks.
Debugging the code as smooth as possible.
It is Awesome, I am using it for past couple of months.
you can change file association which controls what to do when invoking filename in command line. For instance, when you just type text filename in cmd, notepadd will be opened, for word docs Word or other document editor.
Have a look into following link from google or you can fiddle by yourself starting from Control Panel > Control Panel Home > Default Programs > Set Associations. Select a file type in the list and click Change Program.
Alternatively,you can use any of Python IDE (PyCharm,PyDev etc.) so you will be able to run directly scripts from editor.
With Notepad++, you can also create a shortcut to be able to launch your python script :
Menu Run > Run (or press F5)
Choose the python .exe (default in C:\Program Files (x86)\Python36-32\python.exe
Then add this code next to the path : "$(FULL_CURRENT_PATH)"
This will allow to execute the current file
example of command with Python 3.6 :
"C:\Program Files (x86)\Python36-32\python.exe" "$(FULL_CURRENT_PATH)"
Then save the command, by assigning a shorcut key, it's better with a modifier like SHIFT (SHIFT + F10)
Now you can launch any script with this shortcut.
I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).
There are two ways to do this:
Click on a script.
Press command-i to open the "get info" window.
Expand the "Open With" section (if it isn't already).
Choose "Python Launcher" from the drop-down menu.
Click "Change All" if you would like ALL Python scripts to launch when double clicked.
Possibly open Python Launcher and uncheck "Run in a Terminal window"
This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
Validate the Interpreter Directive, that's the first line of the file. I suggest using /usr/bin/env python3. This will run the first python3 interpreter that is on the users path.
Make the script executable chmod a+x <script_name> from the Terminal.
Change the extension from .py to .command (this will be opened by the Terminal).
Use zip or tar for distribution so that the permissions do not get mangled.
This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies> should minimize complications.
I recently tried to convert one of my python scripts (used for file-systems I/O) to a executable file by using py2exe. However, after successfully generating the .exe file from my python script, I am no longer able to copy and paste any text(or anything for that matter) from other windows apps to my python app console (when I run the script/app from the .exe file). When right click the mouse now, the access window does not pop up any longer. Does anyone know how I can get around this issue?
Thanks,
A.L.
Right-clicking a console window is a "special feature" of the command line interpreter of Windows Vista and above. It doesn't work on any other command-line tool, so it doesn't longer works for your converted script. You can still access it using the windows menu (small icon in the title bar or Alt+Space).
You could try to get the right-click feature back by messing around with Windows API calls - but the more convenient possibility would be to enable pasting using Ctrl+V and forget about the window menu. A good candidate is PyReadline - install the package and run this at the beginning of your script:
import readline
readline.parse_and_bind("control-v: paste")
I created a script that will tell me what to wear in the morning based on the weather (i.e. rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click from my desktop and the script will automatically run.
My goal is to be able to simply double click [something] in the morning and it will automatically run the script and thus tell me what to wear. How could I go about doing this?
System Specifications:
python
Mac OSX
This worked for me on Snow Leopard:
-Put the python script on the desktop.
-Right click on the script file, and choose "Get info"
-Find "Open With", and choose "Python Launcher" from the dropdown box
Now double-clicking the script file will run the script in a new terminal window.
I'm not sure what versions of OS X come with the Python Launcher application. If you don't have that, you can solve it with a couple extra steps:
-Put the python script anywhere
-Create a shell script on the desktop with one line:
python "/Users/john/scripts/what-to-wear.py"
(Where I've assumed your script is called what-to-wear.py and is in /Users/john/scripts. Be aware that you do need to use an absolute path.)
-Make the shell script executable. In a terminal:
chmod 755 what-to-wear-shell-script
-Double clicking the shell script should run it in a terminal, running your python script.
What you want to do is create an executable file.
I've never used a Mac or Python, but look at this question and the first answer:
How can I create a directly-executable cross-platform GUI app using Python?
Seems http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html is what you're looking for
Use a batch file to make it automatic
Example :
1. Open Notepad -> type the following.
This one's for Windows..It might give you a hint
:start
C:\Python34\python.exe(your python file location)Your *.py file location.
:end
Save this with a *.bat extension
That's it ..you can configure more on this batch,I guess batch is the automation for day to day script
In Linux/unix based OS , add #!/usr/bin/python3 line on top of your script file with extension .py , if you have python version 3. Or change it to the version installed in the machine
Further , make the file executable by
sudo chmod +x <fileName>
for windows, add windows python path and make the file executable
You want the script to download the weather information online and output the clothes based on your predefined rules?
If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. And write your logic using nested IF THEN ELSE blocks.