Getting custom files to launch like .exe in Windows - python

How does Windows know that an .exe application is an .exe application. Also, how would you tell windows to send this type of application to this program, that you have created. Like how Python programs are .py and are text files, but when you click on it, it acts like an executable. Or is it the other way around? I don't know. Any type of help would be useful, thanks.

In Windows this is different.
There is a variable called PATHEXT in the environment, like this
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
Notice that I've added the .py extension to mark python scripts to be executable. That is the first step.
Then, you need to tell Windows which program will run this script. For that, you have the assoc and ftype commands.
When you type assoc you get the list of known file extensions. Here, I get .py=Python.File as output. Now you have to connect this extension to a program, ie. the python executable python.exe. Do this with ftype:
D:\\>ftype Python.File
Python.File="C:\Program Files (x86)\python27\python.exe" "%1" %*
I think that the correct calls to assoc and ftype are done when installing python. Then you only have to append the extension to PATHEXT.

A .py file doesn't execute when you write $ ./file.py unless you add #!/usr/bin/env python or C:\\Python27\python.exe something like that at the very beginning of it. The OS tries to execute the following: the_thing_you_wrote_after_# file.py If you change the extension to .lold it will work too.
If you try to do ./file.py without adding the line I was talking about earlier, the current shell'll try to parse the text and will definitely give errors.
I'm not quite sure, but Windows checks if it's possible to run a file by checking its extension first. It also searches for the entry point. If you rename your test.exe to test.test it won't work.
You can't launch a .py file like an app unless you use special tools like py2exe and cxfreeze.

Related

How to execute .py file with double-click

I just uninstalled and reinstalled python on my Windows machine. Before I uninstalled my previous version I was able to just double-click on a python script and it would open the command prompt, run the script, and close automatically. After re-installing with the newest version (3.9), I am no longer able to execute the script like that with a double-click.
Clearly I had done something special last time to set that up for myself, but I don't remember what it was. Any idea how I can get that double-click deal going again?
Doing the following should fix it:
Right click on the .py file you want to open;
Open with -> Choose default program -> More options;
Select the python.exe file.
Explanation:
Your Python scripts have to be processed by another program called the Python interpreter. The interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your program.
Installing a new version might have messed the path to the Python interpreter. The steps listed above will tell Windows to associate .py files with your Python interpreter, thus fixing the issue.
This link with Python on Windows FAQ might also be of help.
There will be an option of "Open With" after right-click on the file go and choose CMD. I hope it helps if not then sorry. Because I use Parrot OS
Save the following text to a file called something like python.reg (the .reg extension is important). You might need to modify the last line to be your exact path to python.exe!
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shell\open]
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
#="\"C:\\Python2.7\\python.exe\" \"%1\" %*"
Find the python.reg file you just saved and double-click it to load those contents into your Registry. If you've performed one of the other operations in other answers (like "Open With"), those "UserChoice" settings are stored somewhere else in the Registry and will override the "Classes" setting shown in this script. So, do one or the other, don't combine them!

Can a Script be an Environment Variable

I have a Python script that I would like to be able to execute from no matter where. Either in Linux or in Windows, but in this case preferably in Windows. Putting the path to the script into PATH under Windows did not work, so from some directory calling python my_script.py results in the message that there is no such file in this directory. So, is this somehow possible?
You can try creating an alias as such:
Linux
Create a .bash_aliases file on your home folder
Add an alias such as alias pscript='python /home/pythonscript.py'
Log out and back in or do a source .bash_aliases
Windows
Run doskey pscript=python C:\script.py. Read more here
The PATH is used to search for executables, and this doesn't include in windows script files.
A workaround is to convert the script to a batch file, see here
how to simply have the script act also as a batch file
For the operating system to run your script, it needs to find it (the PATH variable), recognize that it is executable and know which program should execute it. You seem to have the PATH part handled, so now for the other two.
On unixy systems you need to make the script executable. To set the user-executable bit, do chmod u+x myscript.py. Then you need to tell the system which program should run it. Typically you use the "shebang" as the very first line in the file:
#!/usr/bin/env python3
The system will search the path for a program called "python3" (use "python" for python 2 scripts) and use that executable to run the script.
On Windows, you need to associate the file extension (.py) with the python exeuctable. That's usually done for you when python is installed. If not, you can dig into ftype, assoc and pathext here.
Windows doesn't care about the shebang (unless you are running cygwin, then see unixy systems above) so the same script can live in both worlds.
Once the script is executable, you call it directly instead of executing python and giving the file as the script name. Its just
myscript.py

Impossible to set python.exe to *.py scripts on Win7

i've installed py 2.7 (64bit) on my PC with Win7 (64bit) without problem but I'm not able to run *.py scripts via DOS shell without declare python full path.
Let me better explain :
If I type D:\ myscript.py it doesn't work. The script is open with wordpad
If I type D:\ C:\Python27 myscript.py it works and run correctly
I try to change the default application software for *.py file via Win7 GUI ( control pannel etc etc) but without success.
Python is not present in the list of available sw and in any case also with the manual set I'm not able to associate python.exe at *.py files.
I've checked in my environment variables but I've not found problem (python path is declared in Path = C:\Python27\;C:\Python27\Scripts).
I've tried also to modify HKEY_CLASSES_ROOT->Applications->python.exe->shell->open->command :
old register value "C:\Python27\python.exe" "%1"
new register value "C:\Python27\python.exe" "%1" %*
without success.
Any suggestion?
Thanks
Here is another check to make, which helped me figure out what was going on.
I switched from the 32bit Anaconda to the 64bit version. I deinstalled, downloaded then reinstalled, but several things didn't get cleaned up properly (quick launch stuff, and some registry keys). The problem on my side was that the default installation path changed, from C:\Anaconda to C:\Anaconda2.
I first tried the assoc and ftype tricks, everything was fine there. However, the HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command registry key was pointing to the old Anaconda path. As soon as I fixed this, python.exe showed up when I tried associating with "Open with" and everything went back to normal.
I also added the %* at the end in the registry key.
You could try to use the ASSOCIATE command in CMD:
ASSOCIATE .py C:\PathTo\python.exe
More information at http://ss64.com/nt/associate.html
#slv 's answer is good and helped me a bit with solving this problem. Anyhow, since I had previous installations of Python before this error occured for me, I might have to add something to this. One of the main problems hereby was that the directory of my python-installation changed.
So, I opened regedit.exe and followed these to steps:
I searched the entire registry for .py, .pyw, .pyx and .pyc (hopefully I did not forget to mention any here). Then, I radically deleted all occurrences I could find.
I searched the entire registry for my old python-installation-path (e.g. C:\Users\Desktop\Anaconda3). Then I replaced this path with my new installation path (e.g. C:\Users\Desktop\Miniconda3). Thereby, I also came across and replaced HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command which #slv mentioned.
Afterwards, it was possible again to connect a .py-file from the Open with...-menu with my python.exe.
The *.py file is a source code file. If you set up your system environment correctly, you need to run python myscript.py
the following answer is related to your question
Making Python scripts run on Windows without specifying ".py" extension
Do you know that when you change the default application of a file, you are able to browse for the application?
You can click on the "browse" button (as shown in the red rectangle), then choose C:\Python27\python.exe.
Also remember to click on the "Always use this program to open this program" checkbox, which is shown in the green rectangle, so that win7 knows how to open this file the next time you ask it to open *.py file.
Then I believe you'll be able to run myScript.py simply by typing "myScript.py" in the correct dirctory in DOS shell.
After doing all the above steps, be sure to reopen a new command shell.

How do I run a .py script directly from cmd?

Suppose I have a script called 'run.py', how can I do this?
C:\Users\Administrator>run
And this script will be executed. Please notice that I don't want a '.py' shown after 'run'.
I am using python 3.3.5, and I tried putting 'C:/python33' into environment variable path. But it didn't work.
It seems like it only works when it's a '.exe' file.
Thanks in advance.
You will need to add .py to your PATHEXT environment variable.
From the Python on Windows FAQ, How do I make Python scripts executable?
On Windows, the standard Python installer already associates the .py
extension with a file type (Python.File) and gives that file type an
open command that runs the interpreter (D:\Program
Files\Python\python.exe "%1" %*). This is enough to make scripts
executable from the command prompt as ‘foo.py’. If you’d rather be
able to execute the script by simple typing ‘foo’ with no extension
you need to add .py to the PATHEXT environment variable.
The exact method for setting environment variables varies with different versions of Windows, but this link can probably help you out.

Python path help

how can i run my program using test files on my desktop without typing in the specific pathname. I just want to be able to type the file name and continue on with my program. Since i want to be able to send it to a friend and not needing for him to change the path rather just read the exact same file that he has on his desktop.
f = open(os.path.join(os.environ['USERPROFILE'], 'DESKTOP', my_filename))
If you are on Unix/Mac you can add a shebang at the top of your script and set it as executable. Usually you'd do:
#!/usr/bin/env python
And then to make it executable, from a terminal use chmod:
chmod +x script.py
Now you can run the script (if you are in the same directory) like:
./script.py
If you are on Windows you'll want to add the Python executable to your %PATH% enviromental variable, then you can run your script with python script.py. They talk about this in more depth in the Python documentation: http://docs.python.org/using/windows.html
If you place your Python script in the same directory as the files your script is going to open, then you don't need to specify any paths. Be sure to allow the Python installer to "Register Extensions", so Python is called when you double-click on a Python script.
You can tell your friend to make *.py files to be executed by the interpreter. Change it from Explorer:Tools:Folder Options:File Types.

Categories

Resources