I have installed the Enthought Python distribution on my computer, but I don't have any idea how to use it. I have PyLab and IDLE but I want to run .py files by typing the following command:
python fileName.py
I don't know where to write this command: IDLE, PyLab or Python.exe or Windows command prompt. When I do this in IDLE it says:
SyntaxError: invalid syntax
Please help me to figure this out.
Open a command prompt: Press ⊞ Win and R at the same time, then type in cmd and press ↵ Enter
Navigate to the folder where you have the ".py" file (use cd .. to go one folder back or cd folderName to enter folderName)
Then type in python filename.py
Indeed, the command to run a Python file should be run in the command prompt. Python should be in your path variable for it to work flexible.
When the python folder is added to path you can call python everywhere in the command prompt, otherwise just in your python install folder.
The following is from the python website:
Windows has a built-in dialog for changing environment variables
(following guide applies to XP classical view): Right-click the icon
for your machine (usually located on your Desktop and called “My
Computer”) and choose Properties there. Then, open the Advanced tab
and click the Environment Variables button.
In short, your path is:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables In this
dialog, you can add or modify User and System variables. To change
System variables, you need non-restricted access to your machine (i.e.
Administrator rights).
Another way of adding variables to your environment is using the set
command in a command prompt:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
If you do it via My Computer, then look for the line named path in Enviroment Variables. Give that the value of your Python installation folder.
Set PYTHON variable to point to the full path of python.exe.
Then type in command prompt console window:
C:\path_to_folder\ python filename.py
Related
I would like to run my python script by a custom command
Previously, we would have to run
python hello.py
For example, I would type:
hello
in my command prompt
then my computer will run my hello.py
You can create a simple batch file and place it in a folder that is in your system path variable.
Name it $Py.bat
#Echo off
If not "%~1" == "" (
python "%~1.py" 2> nul || (Echo/"%~1.py" failed to execute correctly or was not found.)
) Else Echo/Filename required.
Then run it in cmd.exe using:
$Py hello
Another approach would to be to create a macro using setx
SetX run "python F.py"
SetX adds the variable with it's value to the registry. It is not available in the Session in which it is created, nor in any child environment of that Session.
Execute by expanding the variable with substring modification replacing F with the filename:
%run:F=hello%
There are two pieces to this. The first is the PATHEXT environment variable that stores a list of the file extensions for operation system to execute. When running a command line that does not contain an extension, the system uses the value of this environment variable to determine which extensions to look for and in what order, such as .com first, follow by .exe, .bat, .cmd, which happens to be the default value stored in the PathExt by Windows. On my computer:
C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
If .py is not part of your PATHEXT environment variable, you can add it by right-mouse clicking on the Start box in the lower left corner of your screen and the clicking on Settings. When the new window comes up, type environment in the search box and then click on one of the Edit choices:
This now allows you to type in hello instead of hello.py at the command prompt. Referring back to my environment variable, if there were two files hello.bat and hello.py the system would choose hello.bat because the .BAT extension is listed before the .py extension (everything is case insensitive). But the second piece of the puzzle is associating the Python interpreter with the .py extension so that the system knows how to run that type of file. Enter the Windows ftype command. This produces a list of mappings of file type extensions and the programs used to launch those extensions. On my computer, one of the lines listed is:
Python.CompiledFile="C:\WINDOWS\py.exe" "%L" %*
I installed Python using a standard installation from python.org and when I did I installed the optional Python launcher for Windows, which is the "C:\WINDOWS\py.exe" you see above. This launcher supports multiple Python versions and allows you to control which Python version will execute your Python program based on the shebang that you code as the first line of your program. But generally, when you install Python under Windows, an association is made between the .py file type and the Python interpreter. If you do not have this association, you can look for your Python file in an Explorer window, right-mouse click on it and then choose Open With:
My options show Python since I already have the association. In your case it would probably be missing, so you would pick "Choose Another App" and then if you still don't see Python in the list you would choose "Look for another app on this PC" and then navigate to the Python interpreter. Either way, be sure to click on the box labeled "Always use this app to open .py files":
I trying to run a program in python 3 that uses numpy but it gives me the error ModuleNotFoundError: No module named 'numpy'. I am using Windows 10. I tried running pip install numpy and it says pip is not a recognized command
You need to add python to your environment variables
Computer -> System Properties (or Win+Break) -> Advanced System Settings
Click the Environment variables button (in the Advanced tab)
Edit PATH and append ;C:\Python27 to the end (if you need substitute your Python version)
Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
If you are running python 3.6 you will need to add python to your environment path through command prompt
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt.
To temporarily set environment variables, open Command Prompt and use the set command:
set PATH=C:\Program Files\Python 3.6;%PATH%
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
These changes will apply to any further commands executed in that console and will be inherited by any applications started from the console.
To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).
If you were to run python -v it should now work
This came directly from the python documents section 3.3.1
Now after you restart you should be able to access python through the command line. If you are running 2.7.9+ or 3.4+ pip will come prepackaged with python. However, if you are running an earlier version of python
Per https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip:
Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:
python get-pip.py
You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).
Thanks to How do I install pip on Windows?
Add "way to folder with your python interpreter" and "way to folder with your python interpreter"\Scripts\ to PATH variable. Computer -> Properties -> Extra options -> Environment variables.
I have installed Anaconda on my Windows 10 to install Python 3.6.
I now want to run some Python scripts like test.py.
As far as I understood, I need to initialize some system variables, but I am not really able to get the whole meaning. I can set system variables by typing sysdm.cpl at the start button. Is this the right way? Otherwise?
If you want to set env variables permanently, then yes, this is the right way (start->sysdm.cpl->advanced->Environment variables)
If you want to set some variables for just one particular session, then open cmd.exe and set variables like
set _variable_=_value_
and then run your script.
1- open cmd
2-use cd to go to the file where your script is located(cd.. to go to previous folder and cd name to go to another folder)
3-when you get there in cmd type
4-python filename.py
if you are using anaconda instead of cmd use anaconda prompt
adding python to path:
1-open windows explorer
2-on the left side left click this pc and choose properties
3-then go to advanced system settings
4-then at the bottom enviorment variables
5-if you are windows 10 there will be onedrive written at top and behind it path choose path
6-then click new
7-add python directory to the path and then python directory/scripts
https://www.youtube.com/watch?v=Y2q_b4ugPWk check this
I have just installed Anaconda on my computer because I need to use Numpy.
Well, when I use python I for some reason have to be in the same folder as python.exe and, of course, now that I want to use Anaconda I have to be in the Anaconda3\Scripts folder where python.exe isn't. This is a nightmare, how can I use anaconda with python on a windows computer? Why does it have to be so complicated?
I think you are referring to the command-line use of python?
If you have admin priviliges on your machine you can add python to your environment variables, making it available in the console anywhere. (Sorry for different spellings, I am not on an english machine)
Press Shift+Pause ("System")
Click "Advanced System Options"
Click "Environment variables"
In the lower field with "System variables" there is a variable called PATH. Append the complete path to your python.exe without the file to that by adding a ; behind the last path in the variable and then adding your path. Do not add any spaces!
Example: C:\examplepath\;C:\Python27\
When you install anaconda on windows now, it doesn't automatically add Python or Conda to your path.
If you don’t know where your conda and/or python is, you type the following commands into your anaconda prompt (it comes when you install anaconda)
Next, you can add Python and Conda to your path by using the setx command in your command prompt.
Next close that command prompt and open a new one. You should now be able to use the python command. To do this you open a command prompt and type
python nameofPythonfile.py
Source: https://medium.com/#GalarnykMichael/install-python-on-windows-anaconda-c63c7c3d1444
To be able to do that in the command line you just have to add Python and also the Anaconda3\Scripts directory to your system path.
Here is a good tutorial on setting your path in Windows:
http://www.computerhope.com/issues/ch000549.htm
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