although I have been using python a long time very easily in a Linux environment, I have tremendous trouble to even install it correctly in a windows environment. I hope this is a question to be asked here, as it is not directly a programming question.
Especially, I have the following problems:
When on the command line, python is not a recognized command. Do I have to set the Windows path manually myself? If so, how to do that?
When starting a python script, should this be done with python.exe or pythonw.exe? What is the difference?
I also tried to install ipython several times, it never got installed (even after following the starting ipythonenter link description here thread.
When starting a script with python.exe, a window pops up and closes immediately. I saw some hints in putting in a readline command, which is of no help if there is a syntax error in the script. So how to be able to keep the window open, or how to run the command on the cmd.exe?
Thank you for any help on these items.
Alex
1) Look here: www.computerhope.com/issues/ch000549.htm
2) It has already been answered, always try to use search before asking question:
pythonw.exe or python.exe?
4) When using cmd.exe just navigate to your script folder using dir for changing directories and C:,D:,etc. for changing drives. Then run script by typing just the script name. When installed correctly, Python automatically launches .py scripts with python, so you don't have to write 'python' before script name. When run in cmd, window will stay open. If you want it to stay open even when launching script with double-click, use function waiting for user input, see here How to keep a Python script output window open?
You might want to use Python3.3, there is a new launcher for Python scripts in it. By that, you can start Python scripts with py <scriptname> which has the benefit of being installed in your path (C:\Windows\system32) and you can use a shebang to tell whether the script is for Python2 or Python3.
Also
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH
(contributed by Brian Curtin in issue 3561).
Related
I am currently trying to figure out how to set up using python 3 on my machine (Windows 10 pro 64-bit), but I keep getting stuck.
I used the Python 3.6 downloader to install python, but whenever I try to use Command Prompt it keeps saying "'python' is not recognized as an internal or external command, operable program or batch file" as if I have not yet installed it.
Unlike answers to previous questions, I have already added ";C:\Python36" to my Path environment variable, so what am I doing wrong?
I am relatively new to python, but know how to use it on my Mac, so please let me know if I'm just fundamentally confused about something.
In environmental variables under path, add your python path... you said you already so please ensure is their comma separation between previous path..
And once added save environment variables tab. And close all command prompt then open it.
Then only command prompt will refresh with your python config..
Main thing, if you enter python which mean python 2.
For python3 type, python3 then it should work
Why are you using command prompt? I just use the python shell that comes with IDLE. It’s much simpler.
If you have to use command prompt for some reason, you’re problem is probably that you need to type in python3. Plain python is what you use for using Python 2 in the command prompt.
Thanks everyone, I ended up uninstalling and then re-downloading python, and selecting the button that says "add to environment variables." Previously, I typed the addition to Path myself, so I thought it might make a difference if I included it in the installation process instead. Then, I completely restarted my computer rather than just Command Prompt itself. I'm not sure which of these two things did it, but it works now!
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.
I'm new in Python. I'm using Windows 7.
When I install pip on my computer, I need to use the installing program: get-pip.py.
I found 3 ways to run this .py file:
In cmd, type "python get-pip.py"
Open it with IDLE and F5
Double click get-pip.py
I have two questions:
The only way can install it is "1. In cmd, type "python get-pip.py". I wonder what's the difference between 1 2 and 3?
Usually, when I double click .py file, it is usually opened by idle(default). But this time, when I double click get-pip.py, it begins running as if a .exe file. Why?
Thank you.
Answer to question 1.
If you run python from within a command window (cmd) you will be able to see the output if any. As opposed to double clicking it which will run the program and close it unless the program is supposed to do otherwise. And Idle is mimicking the open command window.
One thing to watch out for is having 2 python interpreters on the system. If you are just starting out I would avoid installing a 2nd one. Removing old python interpreters is as easy as removing the folder they are in. You said it "runs differently" which makes me think this is a concern.
Answer to question 2
You can change what program uses the file. You can have idle read it, or the python interpreter run it. check out http://www.thewindowsclub.com/change-file-associations-windows for info on how to change that behavior.
I am using Python 3.3 (2.7 is also installed) and a compatible version of pygame. Recently I have been trying to switch from IDLE to Notepad++
I am using a saved shortcut in Notepad++
C:\Python33\python.bat "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
which runs the batch file:
#echo off
cd %1
%2
if not errorlevel 1 goto quit
echo.
echo.
pause
:quit
When I run C:\Python33\Foldername\imp_prob.py
import pygame
in IDLE it works fine, in Notepad++ using that shortcut it gives an ImportError: No module named pygame
My questions are:
Why is the NP++ method not producing the same result?
How can I change the shortcut or batch file to make it run stuff that IDLE can run?
What method can I use to ensure that I can import a module regardless of which directory I am running the program from?
edit: a working alternative was in the answers to How do you run a python script from within notepad++?
I had some issues with the code they provided, but replacing "python" with the full path to my python33 install solved that.
I still don't understand why pygame wouldn't import when using my run shortcut. I also don't understand why NppExec works when Run doesn't.
It sounds like you would need to set your systemvariables. Idle does not require these steps. You entered the full path to the python.exe in np++ to execute the python program, but the path to the modules etc. is still unknown.
Add the paths, and try again.
System Properties -> Advanced -> Environment Variables, in the bottom window look for a "Path" variable, Edit and append the following the existing entries (do not delete anything in there!)
;C:\Python33;C:\Python33\DLLs;C:\Python33\Lib
for Python 3.3, if you have installed it into its default directory.
To see if everything worked, open the console anywhere (shift+rightclick -> Open Command Window Here) and just type "python". The python console should open, telling you that you use python 3.3. You then also do not need to tell np++ the full python path, but instead can just use "python" again.
How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]