How to set python enviroment variable on windows - python

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

Related

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.

Command Prompt: Set up for Python 2.7 by default

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,

Run a python script from the prompt in windows [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I run a python program in the Command Prompt in Windows 7?
This is a follow-up to this question: Run a python script in windows.
How would I do the equivalent of
`$ ./checksum.py <folder>
in Windows? Note, the checksum.py file starts thus:
#!/usr/bin/env python
For me, it works just to invoke the name of the script directly, e.g. > myscript.py.
if you have python installed on your system just make sure it's in the global variables.
Then you can type in "python " eg "python myscript.py abcd".
If it's not registered at global level you have to 'cd' (ChangeDir) to the location where python is installed, then run a command "python " eg:
"C:\Programs\Python>python C:\Users\User1\Desktop\MyScript.py abcd" where "C:\Programs\Python" is the current working directory.
If you want to run linux programs and commands on windows you can try MinGW or CygWin.
One potential solution to this problem, while possibly overkill, is to install Cygwin and use its environment to run the script. Of course you can just call the python command from your Windows command line (as long as it's in your PATH, as specified in autoexec.bat) followed by ./checksum.py [folder], but if you're coming from a *nix/OS X environment, you may find Cygwin makes your life simpler. Either way.
Make sure the filename extension .py is associated with the appropriate python.exe. Similarly, .pyw should be associated with pythonw.exe (this is a version of the Python interpreter that doesn't show a terminal window, suitable for use with Python GUI scripts).
The Python for Windows installer does this, so you usually won't have to mess with it unless you have multiple Python installs on your machine. If you do need to change the association, this can be done by right-clicking a .py file, choosing Properties, and clicking the Change button next to "Opens with."
Windows ignores the shebang line, so there is no way (short of Cygwin) to have different scripts use different versions of Python by changing the shebang. You could use a different extension (e.g. .py3 for Python 3 scripts) and associate that with C:\Python31\python.exe -- but that will break the script's ability to be imported as a module (Python expects the .py extension), so use it carefully. Better practice is probably to just specify the desired python.exe directly on the command line.

Running python from the mac terminal

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.

How to associate py extension with python launcher on Mac OS X?

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.

Categories

Resources