Does anyone know how to associate the py extension with the python interpreter on Mac OS X 10.5.7? I have gotten as far as selecting the application with which to associate it (/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python), but the python executable appears as a non-selectable grayed-out item. Any ideas?
The python.org OS X Python installers include an application called "Python Launcher.app" which does exactly what you want. It gets installed into /Applications /Python n.n/ for n.n > 2.6 or /Applications/MacPython n.n/ for 2.5 and earlier. In its preference panel, you can specify which Python executable to launch; it can be any command-line path, including the Apple-installed one at /usr/bin/python2.5. You will also need to ensure that .py is associated with "Python Launcher"; you can use the Finder's Get Info command to do that as described elsewhere. Be aware, though, that this could be a security risk if downloaded .py scripts are automatically launched by your browser(s). (Note, the Apple-supplied Python in 10.5 does not include "Python Launcher.app").
The file associations are done with the "Get Info". You select your .PY file, select the File menu; Get Info menu item.
Mid-way down the Get Info page is "Open With".
You can pick the Python Launcher. There's a Change All.. button that changes the association for all .py files.
Steve, add the following to the top of your python script:
#!/usr/bin/env python
It must occur as the first line of the file.
Then make the file executable as so:
daves-macbookpro ~: chmod +x foo.py
Then all you need to do to run this is type
./foo.py
The default python installation (atleast on 10.6.8) includes the Python Launcher.app in /System/Library/Frameworks/Python.framework/Resources/, which is aliased to the latest/current version of Python installed on the system. This application launches terminal and sets the right environment to run the script.
Related
I have encountered an error while try to run a python 3.6.4 script after upgrading from 2.7.
I had initial installed both python 2.7 and 3.6 but them deleted 2.7 because I had no use for it and i am not getting this error.
I have tried:
Reinstalling python 3.6.4
changing the install location of said install
Repairing the python 3.6 install
Error:
A Python script by itself is not an executable program. So if you want to run the script Evosim.py by just entering Evosim.py in the command line, the .py file ending needs to be associated with the Python interpreter. It seems that currently in your system this is not the case.
There are two ways to deal with your situation:
Start your script by entering python Evosim.py instead of only Evosim.py in the command line.
Fix the broken association of the .py file ending with the Python interpreter. The answers to this question migth help you with that.
Solution #1
Go to your registry by searching regedit.
Open category HKEY_CLASSES_ROOT. Select .py. If .py don't exists then make it by yourself.
Double click the Default. It will make a popup. Change its value to Python.IDLE and click OK. Make a new String key in .py naming Content Type with value text/plain.
Solution #2
If the above method don't works then just go ahead and reinstall your python (also uninstall Python Launcher). Your modules and files will be at their place.
You can now open .py files.
Running command prompt as an administrator resolved my issue, and able to execute my python script
1). Right click any python file
2). Click Open with then Choose another app
3). Click on More apps then Look for another app on this PC. It should open up file explorer
4). Select python.exe wherever it is located.
A sample path is C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe for a user installation. A way to check this path is by typing where python into cmd; it will list all installations of Python.
Now the python.exe you chose is associated with all .py files and typing file.py should not give an error.
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
My command prompt is currently running Python 3.2 by default how do I set it up to run Python 2.7 by default, I have changed the PATH variable to point towards Python 2.7, but that did not work.
UPDATE:
It still does not work. :(
Still running python3 - to be specific it runs python3 when I am trying to install flask - which is what I want to do.
More generally, when I simply type python into the command line, it does nothing.
I get a 'python' is not recognized as an internal or external command, operable program, or batch file error.
No idea what to do.
If you call your Python scripts directly using python script.py then setting the PATH to have the 2.7 directory first should be enough.
If you want to call Python scripts indirectly with the shell, i.e. by writing just script.py or by executing the file from the explorer, you need to set the 2.7 installation as the default program handler for the .py extension. The easiest way to do that is to run the Python installer again and choose the option “make this installation the default”.
Note that with the new launcher, that ships with 3.3+, you don’t need this, as you can specify the version number using a shebang line and the launcher will automatically pick the appropriate interpreter.
Changing your PATH environment variable should do the trick. Some troubleshooting tips:
Make sure you didn't just change the local, but rather the system variable to reflect the new location
Make sure you restarted your CL window (aka close "cmd" or command prompt and reopen it). This will refresh the system variables you just updated.
Make sure you remove all references to C:\Python32\ or whatever the old path was (again, check local and system PATH - they are both found on the same environmental variables window).
Check to see if Python3.2 is installed where you think it is... (just rename the directory to something like OLD_Python3.2 and go to your CLI and enter "python" - does it start up? If it does is it 2.7? or 3.2? If not, you did something wrong with your PATH variable.
All else fails - reboot and try again (you might have some persistent environment variable - which I don't see how that can be - but hey, we are brainstorming here! - and a reboot would give you a fresh start.
If that doesn't work then I'd think you are doing something else wrong (aka user error). CMD has to know where to look for python before it can execute. It knows this from your PATH variable... now granted, I work almost exclusively in 2.6/2.7, so if they did something to the registry (which I doubt) then I wouldn't know about that.
Good luck!
Change the two las lines to you current python desired build path:
Windows Registry Editor Version 5.00
' Extracted from Python 2.7 silent installation By Elektro H#cker
[HKEY_CLASSES_ROOT\.py]
#="Python.File"
[HKEY_CLASSES_ROOT\.pyc]
#="Python.CompiledFile"
[HKEY_CLASSES_ROOT\.pyo]
#="Python.CompiledFile"
[HKEY_CLASSES_ROOT\.pyw]
#="Python.NoConFile"
[HKEY_CLASSES_ROOT\Python.File\Shell\Open\Command]
#="CMD /K \"\"C:\\Program Files (x86)\\Python\\Python.exe\" \"%1\" %*\""
[HKEY_CLASSES_ROOT\Python.NoConFile\Shell\open\command]
#="CMD /K \"\"C:\\Program Files (x86)\\Python\\Pythonw.exe\" \"%1\" %*\""
Could you try this as root:
$ ln -s python2.7 /usr/local/bin/python
Logout & login again as root:
$ python -V
It works for me, hoping the same to you.
Cheers,
I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.
I've downloaded python installer from http://www.python.org/ftp/python/3.1.2/ , this python-3.1.2.msi file, I need to execute some python files? How do I do that? For example in php I'd do php filename.php from console, I do however have python command line but I don't know how to execute those files.
So if I could set ENV variable to directly execute my file(s) if that is possible that would be great.
There is an option in the installer called "Register Extensions" to associate Python files with the interpreter, so double-clicking them or entering filename.py in the console should work.
Apart from that you might want to add C:\Python31 to your PATH variable (right-click on My Computer, choose Settings, choose the Advanced Tab - there you can access the system variables. Better do this as an admin.
If you type python in the Windows command line, what happens? Is the Python interpreter in your PATH yet?
If not, add the Python installation directory there (here's a good guide). Then just do python script.py just like with PHP.
you can just execute
python yourfile.py
Or if the python command don't work you have to give the absolute path to you python installation or add it to windows path