How to launch a Python file on Windows 7? - python

this is the first time I have used Python.
I downloaded the file ActivePython-2.7.1.4-win32-x86
and installed it on my computer; I'm using Win7.
So when I tried to run a python program, it appears and disappears very quickly. I don't have enough time to see anything on the screen. I just downloaded the file and double-cliked on it.
How do I launch this file? I know that it is a long file for a first Python tutorial.

Add the line
input()
to the end of the program, with the correct indentation. The issue is that after the data is printed to the console the program finishes, so the console goes away. input tells the program to wait for input, so the console won't be closed when it finishes printing.
I hope you're not using that program to learn Python; it's pretty complicated!

go to Start > All programs > Accessories and click on Command Prompt. then drag the python file from the explorer view into this command line and press Enter...
now you can watch the output of the script execution !

run it from a command prompt:
> python myscript.py
You can also start only the python interpreter from the command prompt (or by running python.exe) and then try some commands:
> python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> a = 2
>>> b = 7
>>> print a+b
9
>>>

Or run it from a batch file:
myprog.py
pause
Has the advantage that you can specify a different version of Python too.

Just a bit more on this.
You have a script myscript.py in a folder C:\myscripts. This is how to set up Windows 7 so that you can type > myscript into a CMD window and the script will run.
1) Set your PATH variable to include the Python Interpreter.
Control Panel > System and Security > System > Advanced Settings > Environment Variables. You can set either the System Variables or the User Variables. Scroll down till you find PATH, select it, click Edit.The Path appears selected in a new dialog. I always copy it into Notepad to edit it though all you need do is add ;C:\Python27 to the end of the list. Save this.
2) Set your PATH variable to include C:\myscripts
3) Set your PATHEXT variable to include ;.PY. (This is the bit that saves you from typing myscript.py)
This may now just work. Try opening a command window and typing myscript
But it may not. Windows can still mess you about. I had installed and then uninstalled a Python package and when I typed myscript Windows opened a box asking me which program to use. I browsed for C:\python27\python.exe and clicked that. Windows opened another command window ran the script and closed it before I could see what my script had done! To fix this when Windows opens its dialog select your Python and click the "Always do this" checkbox at the bottom. Then it doesn't open and close another window and things work as they should. Or they did for me.
Added: Above does not say how to pass arguments to your script. For this see answer Windows fails to pass arguments to python script

Related

Visual Studio Code Terminal keeps running Python script in Powershell

I recently installed both Python and Visual Studio Code. After taking an intro class I wrote a basic script and ran it in Visual Studio Code. That's when I noticed a problem with the way Python is setup in my Visual Studio code.
Problem:
When I start Visual Studio Code and open a python file, the Terminal defaults to "C:\Users\my_name\Documents Python" (this is the folder my python files are stored in). From what I understand, when you're in Python, the prompt should be ">>>". I am able to run my script but i cannot run any other Python code (ie something as simple as z = 5) in the terminal. If I type in "Python", I am prompted with ">>>" but can no longer run my script.
I thought this was an installation issue so i uninstalled and reinstalled both Python and Visual but the problem persists.
I tried adding the Python file path to where the program is installed to the windows environment under system settings and also clicked "Add to Path" when reinstalling Python but none of these solutions seemed to work.
when basic python code (ie z=5) doesn't seem to work but the script runs fine I get the error message below:
PS C:\Users\my_name\Documents\Python 2> z=5
z=5 : The term 'z=5' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ z=5
+ ~~~
+ CategoryInfo : ObjectNotFound: (z=5:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Below is the error message I get after I switch to python and try running my script
PS C:\Users\my_name\Documents\Python 2> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> & C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
File "<stdin>", line 1
& C:/Users/my_name/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/my_name/Documents/Python 2/new2.py"
^
SyntaxError: invalid syntax
>>>
There is nothing wrong with your setup. It looks you miss basic understanding of different ways to execute python code.
VS Code has integrated terminal. From there You can run your python script i.e. file with py extension same as you are on terminal/cmd/powershell. That is common way to write and execute code.
When you type python and hit enter you start python interactive shell. That is when you get >>> prompt. Your python interpreter evaluate and execute each line as you type in and hit enter. Same will happen if you type in python in cmd/powershell outside VS Code. Interactive mode is used more or less to experiment, test simple ideas, simple code examples, etc. but the code you type in is lost once you exit the interactive mode by >>>exit().
for further reference you may check
VSCode from start
How to execute python code
After reading the documentation above, I also had a similar problem after running scripts. I think that VSC is exiting out of python after running a script and requires starting up the python terminal again. I wonder if VSC can default to python after running a script instead of exiting out to powershell.
I figured out what the issue was here. Visual code thought Python was installed in the directory that my .py files are saved in as opposed to the actual location under program files. The path had to be edited under settings.
When you are in debug mode and stopped, you can type interactive Python in the 'Debug Window'. Useful for inspecting objects, variables, etc.
Otherwise as suggested, I just type "Python" in the terminal to get the interactive Python prompt ('>>>').

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

How do I open Python IDLE (Shell WIndow) in WIndows 10?

I am just starting to learn Python and I am using Windows 10. I downloaded and installed Python 3.4.3. But everytime I open Python from my Desktop or from C:\Python\python.exe it just opens a black command prompt without any Menu options like File menu, Edit Menu, Format Menu etc. I can't see any colors of the code, it's just black screen with white text. I searched about it on internet and came to know that what I am opening is the Editor winodws and I need to open Shell Window in order to have access to all of those options and features. I can't figure out where is the .exe of Shell Window and with what name is it? Please help me.
P.S. I also tried to open pythonw.exe that was present in the Python folder where it was installed, but nothing opened.
In Windows you will need to right click a .py, and press Edit to edit the file using IDLE. Since the default action of double clicking a .py is executing the file with python on a shell prompt.
To open just IDLE:
Click on that. C:\Python36\Lib\idlelib\idle.bat
Start menu > type IDLE (Python 3.4.3 <bitnum>-bit).
Replace <bitnum> with 32 if 32-bit, otherwise 64.
Example:
IDLE (Python 3.6.2 64-bit)
I agree with one who says:
just type "IDLE" in the start-menu where it says "Type here to search" and press [{ENTER}]
If your using Windows 10 just type in idle where it says: "Type here for search"
My solution to setting options and then invoking Idle on a python script is:
Set optn=blah
...
Set optn=blah
start pythonw C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python36-32\Lib\idlelib\idle.py STFxlate.py
This allows you to setup the environment prior to invoking idle.
This assumes that pythonw is in the current path
For those using Anaconda, type idle on windows search bar ("Run or Execute command"). This probably wont work if you didn't install anaconda with environment variables. You can also go to
Anaconda3 folder > Scripts >idle.exe
and create a shortcut to you desktop.

Trying to directly run .py file using Enthought Canopy Python on Windows 7

I am trying to set up Canopy with Windows so that it will not, by default, try to open as a GUI. Essentially I want to do exactly this:
http://docs.enthought.com/canopy/configure/canopy-cli.html#create-epd-dist
The only problem is that the instructions listed here do not seem to work as expected.
Here's what I want to happen. I have a folder full of scripts. The folder (my_program/) is in my path. I want to call script_1.py on the command line from any directory and have it execute. With the old Enthought distribution this is what happens. It is also what happens with Canopy on Mac. However, running Canopy on Windows, instead the Canopy GUI pops up with script_1.py open for editing.
I ran this command as in the docs:
Canopy\App\Canopy_cli.exe setup C:\Python27 --set-default
It created a "Canopy 64 bit (standalone) Command Prompt" in addition to the regular Command Prompt. However, the behavior in both this new Command Prompt and the regular Command Prompt remains the same -- script_1.py simply opens the Canopy GUI.
Here is my PATH variable:
c:\python27\scripts;
c:\users\****\appdata\local\enthought\canopy\user\scripts;
C:\Users\****\AppData\Local\Enthought\Canopy\User;
C:\Users\****\AppData\Local\Enthought\Canopy\User\Scripts;
C:\Python27;
C:\Python27\Scripts;
C:\Users\****\Documents\my_program;
Any ideas why I'm not able to set up Canopy as an EPD-like environment, or how to do so? Thanks!
Update:
First, I ran this command as Jonathan March outlined in his answer:
ftype Python.File=c:\Users\**\AppData\Local\enthought\Canopy\User\Scripts\python.exe %1
However, with this the behavior remained the same. The above turned out to be half of the solution.
Fortunately, I was able to get it working using regedit. I had to edit Computer\HKEY_CLASSES_ROOT.py by changing the default from "Enthought.Canopy" to "Python.File". And I also changed Computer\HKEY_CLASSES_ROOT.py\OpenWithProgIds by deleting the "Enthought.Canopy" key and adding a "Python.File" key.
I'm really happy to have it working, however mucking around with regedit is a difficult task for Windows users of my software. Now I'm wondering, is there a simpler way to do this, perhaps in the install script that puts my folder of command-line scripts into their path? Any ideas are appreciated.
Sorry for the confusion -- "EPD-like" refers to running python directly, not to the windows file associations, but I can definitely see your point. Here's how to get what you want:
1) Even if you are a full admin on this system, open an "Administrator:Command Prompt" window as described in in steps 1 and 2 in this article: https://support.enthought.com/entries/23736288-Windows-On-some-systems-admin-users-cannot-immediately-install-for-all-users-
2) In this window, type the following command at the command prompt:
ftype Python.File=c:\Users\**\AppData\Local\enthought\Canopy\User\Scripts\python.exe %1
Test that it now works as desired, then close this window.

How to make shortcut work from PATH

I have two versions of Python on Windows and want to use them through cmd. I tried to make shortcuts of their python.exe and renaming them to python26 and python33 (I also added their locations to PATH), but unfortunately this does not work. Calling python26 or python26.lnk outputs in not recognized as an internal command.
Is there any other way to do it (like Linux virtualenv), or I missed something in my idea ?
Create a new .bat file under C:\imagaginary_path\ and name it python2.bat.
Within the bat file write:
C:\Python26\python.exe %*
Then create another one under C:\imagaginary_path\ and name it python3.bat.
With the content:
C:\Python33\python.exe %*
Now remove C:\Python26\ and C:\Python33\ from your PATH and instead place C:\imaginary_path\ in your PATH variable.
There, Windows treats .bat files as executables, and now you can call python2 test.py
Now every time you press Ctrl+C You will get a promt asking "Terminate batch job ?" which is kind of annoying, there's a few alternatives in order to solve this and one being you edit your python2.bat to look like:
start C:\Python26\python.exe %*
As others have mentioned, creating a batch file works fine. But if you still want to use normal shortcuts (.lnk files) you can modify your PATHEXT environment variable to include .LNK. This variable tells Windows what extensions to treat as executable files when searching through the PATH variable.
For example, after creating a shortcut and adding its folder to PATH, this works:
C:\>python27
'python27' is not recognized as an internal or external command,
operable program or batch file.
C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
C:\>set PATHEXT=%PATHEXT%;.LNK
C:\>python27
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
C:\>
You could also work with Windows path:
set path=C:\Python26;.;..;C:\windows;C:\windows\system32
prompt $ & start title Python26
Save this as Py26.bat and type Python in the screen that displays
set path=C:\Python33;.;..;C:\windows;C:\windows\system32
prompt $ & start title Python33
Save this as Py33.bat and type Python in the screen that displays
Instead of creating shortcuts, what you could do is to change the name of python.exe itself.
So you could rename the python.exe in C:\Python26\ to 'python2', and the python.exe in C:\Python33\ to 'python3'. Given that most of the Python code right now is in < Python3, an efficient alternative would be to just change the Python 3.3 python.exe file to be 'python3', and leave the Python 2.6 one unchanged. This way, you would be able to specify if you wanted to run something using python3.
Now edit the %PATH% environment variable to include both C:\Python26\ and C:\Python33.
Example:
python3 chunky_bacon_FTW.py
would run using Python3.3.

Categories