How can I get python in the command prompt on Windows? - python

I have just installed Python on my Windows 7. I thought that after that I will be able to run python on the command prompt but it is not the case. After the installation I also found out that I can run the python command shell. This is nice. But what should I do if I want to save my program in a file and then I want to run this program (in Linux, for example, I typed "python file_name.py" in the command line).

You need to add the python bin directory to your path. Follow the instructions here and add c:\python26\bin to the path (unless you installed python in a non-default location).

Is python.exe in your windows path? Try to look at the PATH environment variable and see if the installation folder of python is listed there.

You need to update your environment variables to include the path to the Python executable.
On XP you can do this by right clicking on "My Computer" -> Properties and then going to the "Advanced" tab.

Related

How to identify python in windows 10

Recently I have install python 3.9.9 in my windows 10.it want show the path
I have typed cmd promt "Wchich Python" it want show
Just type python or python3 in cmd
You can use in your cmd
where python
It will show you the path of all installed python in your device
Just a simple command,
where Python
In Command Prompt, either which python or where python will print the path to your python executable.
If which python or where python does not show the path to your Python executable it is likely that it is not in your PATH variable.
To add your executable to the PATH variable you, search for Environment Variables in the Settings application. This will open the Advanced tab in System Properties. Click the Environment Variables button towards the bottom. You can then edit the PATH variable to include the path to your Python executable. Once you have applied the changes and restarted Command Prompt you can then run which python or where python to confirm your changes have taken effect.

Python and the PATH

I have two questions about Python as it relates to PATH. First, I wanted to show you how my computer is set up. Right now I have python stored directly on my C drive like so:
The folder "Python" is Python 3.8.3. Now, here are the folders in my PATH environment variable:
My first question is why is that when I type "python" on my terminal (at any path), it launches the python.exe? As of right now, pip install commands and the python command works no matter what path I navigate. I thought that in order for this to happen, you would have to add the folder to where the python.exe was stored to your PATH variable (and the same goes for pip script). To my understanding, python is no where to be found in my environment variable set-up.
My second question is how would I make it to where Python 3.9.1 launches when I type "python" in the command prompt instead of the older version? Likewise, I would like to be able to use this newer version of pip when I type "pip install blabla."
My first question is why is that when I type "python" on my terminal (at any path), it launches the python.exe? As of right now, pip install commands and the python command works no matter what path I navigate. I thought that in order for this to happen, you would have to add the folder to where the python.exe was stored to your PATH variable (and the same goes for pip script). To my understanding, python is no where to be found in my environment variable set-up.
To answer why it works currently, it appears your screenshot is showing your system variables, but it is possible that your Path set in your user variables on your system point to your C:\Python directory. Check your User variables to make sure it isn't set there.
My second question is how would I make it to where Python 3.9.1 launches when I type "python" in the command prompt instead of the older version? Likewise, I would like to be able to use this newer version of pip when I type "pip install blabla."
I would direct you to the python configuration docs. It talks about how to setup your environment to point to the new installation of python. In short, you'd modify your path variable to point to the directory where the new version of python.exe is saved. You also want to add the \Scripts directory that can be found in that directory to your path. That will allow you to use the expected version of pip from the command prompt.

Why doesn't the command Python not do anything in Command Prompt?

The Command Prompt does not do anything when I use the Python command to execute a module. It does not even do anything when I use it at all; it just pretends that I did not do anything, and goes back to the current working directory. Attached is a screen shot for clarity.
Apparently, if you call python from Windows' Command Prompt (at least with my home computer), it will look for any python version installed using Windows Store, so if you just type python, it will open the Windows Store for you to install.
If you have Python installed, try to call full path to executable or try to add the path containing your Python executable (in my case, C:\Python38) to the system environment variable PATH:
Search for "Edit the system environment variables".
Click the Environment variables... button (in the Advanced tab)
Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
Click OK and reopen cmd for change to take effect.
Update.
I simply uninstalled and reinstalled Python. Once I did that, the program works as it should in CMD.
If you have python already installed, you should just be able to type 'python' alone then press enter. It should then give you a space to execute python code. It could be simple fix, but if not it's still worth a shot.
When its the first time you're going to install python on Windows it shows you the option Add to path unchecked, so you just need to check that option and install python, you can uninstall python and installet again with the check, this fix the problem.
I just use the IDLE, it is the easiest way to run python scripts. Just open up IDLE and click on File < Open.. < and select your file. Then make any last minute changes and click on File < Save. Click on Run < Run Module (or F5) to run the python script. Done.
This is great for people that want to use python 2.7 or just don't want to install the python application from the Microsoft store.
Sometimes the order in which the PATH environment variable is set also matters; if the Windows store variable is before the path where Python is installed then the windows store will open when you type python.

The Python executable is not recognized on Windows 10

I recently installed Python 3.6.3 on my device. When I type python in my cmd window, it gives me this error. I do have the PATH in the environment variables. Path Lists. This should normally fix it, but python is still not recognized.
I had the same problem, due to a stupid decision from Microsoft.
I had another line in my system path variable:
C:\Users\MyUserName\AppData\Local\Microsoft\WindowsApps
And Windows 10 put a python.exe file there that only redirected me to the Python page of the Microsoft Store:
Since this line was above my true Python path, typing python in the cmd prompt opened the Microsoft Store...
Solution:
Type App execution aliases in the Windows 10 search bar, and then uncheck the aliases for python.exe and python3.exe. More information is here.
The path to the Python executable needs to be in the System PATH variable. Note this is not the User PATH variable.
The OP had Python 3.6.3 installed in C:\Python\Python36-32, however a common default location for installation is in the users AppData\Local\Programs\ folder. This post will assume a Python 3.10 installation in this location. Please adjust as needed for your current Python version and path.
You can confirm the path from the command prompt by checking the Python executable directly from the folder.
cd %LOCALAPPDATA%\Programs\Python\Python310
python --version
This should print the Python interpreter version. For example, Python 3.10.4.
Add the following entries to the System PATH:
%LOCALAPPDATA%\Programs\Python\Python310 for the python executable
%LOCALAPPDATA%\Programs\Python\Python310\Scripts for tools such as pip
After adding the path to the System PATH variable, make sure you close and reopen any command prompts, so they use the updated PATH.
If it is still not working, as mentioned in the previous answers, then simply move up the path, as shown in the screenshot.
You get this error, because the python.exe path has not been added to the System environment variable. To do that, simply:
go to the path C:\Users\%Username%\AppData\Local\Programs\Python\Python37-32 which contains python.exe
copy the path and open System environment variables and in the variable section look for variable called Path. If not, create a new variable with Variable Name as Path and value as the copied path
Once you do that, close the window and simply open cmd. Type python and you should get something like this:
If you are trying to install the new Python version, python-3.9.6, then click the checkbox of Add Python 3.9 to PATH
This happens because the path doesn't exist in environment variables.
To fix this:
Rerun the installer
Choose Modify
In optional feature click "Next"
In advanced option tick the "Add Python to environment variables"
Install
All the answers on Stack Overflow_ about it were obsolete, so I decided to add this. The path variable that needs to be added is,
C:\Users\Username\AppData\Local\Programs\Python\Python36-32
Note that everyone may have a different Username.
If you are working with the command prompt and if you are facing the issue even after adding the Python path to system variable PATH:
Remember to restart the command prompt (cmde.exe).
I too was struggling with this issue, and everything was configured correctly. I installed and setup my system path variables as one would. And everything was fine. Then upon reboot windows would not recognize 'python' as a command as if it were unaware of the path variable. Double checked to confirm everything was still setup correctly (it was).
It only started to work again after I manually browsed to the directory in which python.exe existed, and ran 'python'. After that windows seemed to recognize my path variable and I could call 'python' from where ever need be. Very odd. Figured I'd share in case someone else runs into this.
For Python 3.6, there are actually two path entries. Also, it's best to use the %USERPROFILE% rather than hard coding it.
%USERPROFILE%\AppData\Local\Programs\Python\Python36
%USERPROFILE%\AppData\Local\Programs\Python\Python36\Scripts

How can I execute Python scripts using Anaconda's version of Python?

I recently downloaded the Anaconda distribution for Python. I noticed that if I write and execute a Python script (by double-clicking on its icon), my computer (running on Windows 8) will execute it using my old version of Python rather than Anaconda's version. So for example, if my script contains import matplotlib, I will receive an error. Is there a way to get my scripts to use Anaconda's version of Python instead?
I know that I can just open Anaconda's version of Python in the command prompt and manually import it, but I'd like to set things us so that I can just double-click on a .py file and Anaconda's version of Python is automatically used.
I know this is old, but none of the answers here is a real solution if you want to be able to double-click Python files and have the correct interpreter used without modifying your PYTHONPATH or PATH every time you want to use a different interpreter. Sure, from the command line, activate my-environment works, but OP specifically asked about double-clicking.
In this case, the correct thing to do is use the Python launcher for Windows. Then, all you have to do is add #! path\to\interpreter\python.exe to the top of your script. Unfortunately, although the launcher comes standard with Python 3.3+, it is not included with Anaconda (see Python & Windows: Where is the python launcher?), and the simplest thing to do is to install it separately from here.
Set your python path to the Anaconda version instead
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).
Find your PATH variable and add the location of your Anaconda directory.
Example of someone doing it here: How to add to the PYTHONPATH in Windows, so it finds my modules/packages? Make sure that you sub path out for the Anaconda file though.
The instructions in the official Python documentation worked for me: https://docs.python.org/2/using/windows.html#executing-scripts
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
The example shows how to associate the .py extension with the .pyw executable, but it works if you want to associate the .py extension with the Anaconda Python executable. You need administrative rights. The name "Python.File" could be anything, you just have to make sure is the same name in the ftype command. When you finish and before you try double-clicking the .py file, you must change the "Open with" in the file properties. The file type will be now ".py" and it is opened with the Anaconda python.exe.
I like to run a "bare-bones" version of Python 2 to verify scripts that I create for other people without an advanced python setup. But Anaconda and Python 3 have a lot of nice features. To enjoy both things on the same computer I do this on my Windows computer which allows me to easily switch.
C:\Users>python --version
Python 2.7.11
C:\Users>conda create --name p3 python=3
C:\Users>conda info --envs
Using Anaconda Cloud api site https://api.anaconda.org
# conda environments:
#
p3 C:\Anaconda3\envs\p3
root * C:\Anaconda3
C:\Users>activate p3
Deactivating environment "C:\Anaconda3"...
Activating environment "C:\Anaconda3\envs\p3"...
[p3] C:\Users>python --version
Python 3.5.1 :: Continuum Analytics, Inc.
For more info: http://conda.pydata.org/docs/test-drive.html
I know this is an old post, but I recently came across with the same problem. However, adding Anaconda to PYTHONPATH wasn't working for me. What got it fixed was the following:
Added Anaconda to the PYTHONPATH and remove any other distribution of Python from any paths.
Opened the command prompt and started python (Here I had to verify that it was indeed running under the Anaconda dist)
Ran the following lines inside anaconda
>>> import sys
>>> sys.path
['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
Copied the displayed path
Within the script that I'm trying to execute on double click, changed the path to the previously copied one.
import sys
sys.path =['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
Changed the default application for the script to 'python'
After doing this, my scripts are working on double click.
This solution is not perfectly answering the question because it modifies your .py file into a .bat file but it serves the save purpose, so I think it can help few people out there.
To launch a python script with anaconda's environment : change your .py extension into a .bat extension and add this command line in the beginning of your script :
#echo off & call conda activate <env name> & python -x "%~f0" %* & goto :eof
It will launch a .bat file that will activate your conda environment and execute your python script. This allows you to have a single file that you can copy/paste anywhere and just double click on it to launch it.
Here is an example of the content of the ".bat" file :
#echo off & call conda activate my_environment & python -x "%~f0" %* & goto :eof
print("Hello World!")
input()
import pandas as pd
print('succes')
input()
Hope this helps some people ! Cheers
Note:
If anaconda is not in yourr PATH environment variable, just replace :
conda activate with the path of anaconda activate .bat file, it should look like this C:\ProgramData\Anaconda3\Scripts\activate.bat.
You can also replace the environment name with it's directory, it often looks like that :
C:\Users\<user name>\.conda\envs\<environment name>
Many thanks to this post that helped me figure out this solution:
Python command line -x option
don't know windows 8 but you can probably set the default prog for a specific extension, for example on windows 7 you do right click => open with, then you select the prog you want and select 'use this prog as default', or you can remove your old version of python from your path and add the one of the anaconda
You can try to change the default .py program via policy management. Go to windows, search for regedit, right click it. And then run as administrator. Then, you can search the key word "python.exe" And change your Python27 path to you Anaconda path.
I know that this is an old question, but still there is no accepted answer.
Here is what I'm doing to start a python script with a double click in a specific anaconda environment.
I have found a very useful script in the anaconda3 folder cwp.py. This doesn't activate an environment, but it adds the environment prefix to the path.
You should use it as follow:
python cwp.py PREFIX ARGS
So actually, assuming that your anaconda installation is in C:\Users\User\anaconda3 and your environment is in C:\Users\User\anaconda3\env\myenv then what you have to do is to write the following line in a windows shortcut as a target:
C:\Users\User\anaconda3\pythonw.exe C:\Users\User\anaconda3\cwp.py C:\Users\User\anaconda3\env\myenv C:\Users\User\anaconda3\env\myenv\pythonw.exe path_to_your_script\your-script.py
A double click on the shortcut will start your script!

Categories