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.
Related
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 ('>>>').
I'd like to know if there is a way to shorten what must be written on the cmd.exe command line to run Python programs. As it is, I have a program called Calculator.py and if I wanted to run that I would have to write:
python.exe C:\Users\user_name\restofdirectory\Calculator.py
Basically I want to know if I could make the 'Python' folder on my computer default so I could just type python.exe Calculator.py or some other similarly short way of doing it so I didn't have to type the whole directory.
Create a cmd.exe shortcut somewhere handy (Desktop or whatever). Right Click, select properties. Under the Shortcut tab you'll have a "Start In" field. It'll be default to wherever your cmd.exe normally opens. But you can change it to any path you want (So set it to your python development folder)-- then opening that shortcut will always start cmd.exe in the python folder.
You don't need to type python.exe file.py --
Just type: python file.py
So in short, you'd click that shortcut and type python file.py and you are done.
If you have specific scripts you want to run frequently, you can create a cmd.exe shortcut for each one. Set the Start In path to their folder. Then update the link to cmd.exe like this:
c:\path\to\cmd.exe /k python file.py
That will open a cmd prompt and automatically run that specific script
Yes, you can cd into the folder first.
If python is in your default path you can create a folder and put all your python scripts in it. Then cd into that folder and type python Calculator.py. To test if python is in your path simply open up a cmd and type cd \ (which in your case should change your working directory to C:\) then type python. If you get an error saying the command python cannot be found then you have to add python to your path. Other wise you should be able to run cd C:\Users\user_name\restofdirectory\ and then execute the script python Calculator.py
create an environment variable, say, PyPath = C:\Users\user_name\restofdirectory
then your line would be:
python.exe %PyPath%\Calculator.py
or you just cd to C:\Users\user_name\restofdirectory, and run
python.exe Calculator.py
Add Python to your path:
SET PATH=%PATH%;C:\Python27\bin
then you can call python like this:
python myapp.py
If you have the Python Launcher for Windows (comes with Python 3.3+) installed, then it gets even easier. You just need to put shebang in your code:
#! python
The first time you run your code, Windows will pop up a dialog and ask you what program to use to run the script. The Python Launcher should be listed. Just choose that and away you go. Here's a fun little script from the page I linked earlier:
#! python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
Once my Windows 7 box had the launcher installed and I told it what program to run, I could just call my script from cmd.exe like this:
my_script.py
Note: If you have a version of Python older than 3.3, you can download the installer here
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.
NEW Edit. I am using Python2.7.5 in Windows 7. I'm new to command line arguments. I am trying to do this exercise:
Write a program that reads in a string on the command line and returns a table of the letters which occur in the string with the number of times each letter occurs. For example:
$ python letter_counts.py "ThiS is String with Upper and lower case Letters."
a 2
c 1
d 1
# etc.
I have some fundamental problems: How do I add strings to the command line?
I went to Control Panel and altered the Path (adding ;C:\Python27)
Now I'm in cmd.exe
This gets the right answer
>>>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
However these get SyntaxError: invalid syntax:
python letter_counts.py
C:\Python27\python.exe
C:\Python27\pythonw.exe
C:\Python27\python.exe .\letter_counts.py
Any suggestions? thank you!
In response to your question on 'adding strings to the command line'.
import sys
print sys.argv
will give you the command line args you passed in. In your case, it looks like you want the first argument, so you would do something like:
print sys.argv[1]
note that sys.argv[0] will give you the filename you passed into python, so you actually want [1] in this case.
I don't use Windows, but if you want an easy setup python environment you could look into ActivePython perhaps. Worst case or as a temporary solution you could just go to the folder where python.exe is located and run your scripts there.
After modifying your path, did you start up a new instance of the command prompt (this has caused me much grief in the past)? To check if your path change was successful:
echo %PATH%
and check to see if C:\Python2.7.5 was appended to it.
If not then run:
set PATH=%PATH%;C:\Python2.7.5
To call a python script with a command line argument (assuming that your script is in the current working directory):
python letter_counts.py "Your Text Goes Here"
OR if you just want to be overly verbose (or your PATH still isn't right):
C:\Python2.7.5\python.exe .\letter_counts.py "Your text still goes here"
EDIT:
Make sure you are doing this from the command prompt and not the python interpreter. To access the command prompt you can hit:
1. Windows Key + R
2. type "cmd" (without quotes) and hit enter
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