This question already has answers here:
Adding Python to PATH on Windows
(22 answers)
Closed 2 years ago.
Just dipped my toes into Python this morning and I'm trying to pare down how long of a directory I have to input to run my *.py programs.
My Python install directory is C:\Python\Python39
My work folder is C:\python_work
I made a file called hello_world.py and would like to run it directly from the C:\python_work folder, but it won't let me. It just returns C:\python_work instead of "Hello Python World!".
The book I'm following says I should be able to type C:\python_work>python hello_world.py. That doesn't work. I have to type C:\python_work>C:\Python\Python39\python hello_world.py.
How do I get python to run directly from the python_work directory? I hope what I've written has made sense.
Thanks!
EDIT: PATH option is selected on install. Did that before coming here.
I think the issue you are having is that you do not have python added to your PATH.
You can add it to your PATH by using this set of instructions: https://www.educative.io/edpresso/how-to-add-python-to-path-variable-in-windows.
Or you may want to uninstall and then reinstall python ensuring you check the add to PATH button in the installer.
What happens if you type python --version into your command line? If it returns an error, it likely means that you haven't added Python to your system path.
If you type variables into your windows search, you'll see the entry below. Click on that and add your python directory (C:\Python\Python39\) into the entry called PATH.
Once python is in your path, you should be able to open the python work directory and simply run it with the python command.
# in a new terminal
cd C:\python_work
python hello_world.py
You could try the following:
Write in command line: python --version the output must be some like:
Python 3.8.5
If appers:
'python' is not recognized as an internal or external command,
operable program or batch file.
You will need add Python to Windows Path variable Add Python to the Windows Path.
To execute your program, you can try with:
> python C:\python_work\hello_world.py
or:
C:\python_work>python hello_world.py
That's because you do not have python on the PATH. To add the python to path see this answer. Of course, you will be doing it for Python3 rather than 2, but the steps are the same. You can find another proper answer from the same topic.
Related
This question already has answers here:
How can I add a default path to look for python script files in?
(4 answers)
Closed 3 years ago.
Default python folder: c:/python
My script file name.py is in that folder.
Tested my script in the shell and works.
When i do python name.py is gives me syntax error in cmd
Now when i changed command prompt start in location to the python folder it worked. Is there a way i dont have to do that but for it to still open if i do python name.py?
Or what do you all recommend?
When i just type python it shows version etc. So i know the variables for that are correct...
So i have no clue
You can update your PYTHONPATH environment variable to the directory containing your script C:/python. See the answer to this question.
However, this is not the typical, recommended way to find your Python scripts. It is best to use something like this:
import sys
sys.path.append('C:/python')
This question already has answers here:
How can I add python to cmd in windows [closed]
(2 answers)
Closed 2 years ago.
The Chinese words read 'python' is not recognized as an internal or external command operable program or batch file.
I followed instructions on the Internet to solve this and did the followings. However, python is still not recognized. My computer runs windows 10 and my python version is 3.7.
I am a complete novice in programming. Your help means a lot to me. Thank you in advance.
(C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python3.7)
Since Python 3.3, the installer automatically installs the py launcher on your path, and since 3.6, it doesn't put python itself on your path unless you enable a checkbox in the installer.
And that's fine, because you usually want to use py rather than python anyway. See Python Launcher for Windows in the help.
If you really need python on your path, the recommended solution is to just re-run the installer, select Modify, and click the checkbox. If you want to do it manually, you can, but it really isn't necessary. See Finding the Python executable in the docs for details.
You probably did not add Python to PATH
Look for your installed Python inside C:\Program Files folder
Take note of the path of Python
Ex. C:\Program Files\Pyhton37
Adding python directory to PATH
Right click on This PC
Then click on Properties
At the pop up window, look for Environment Variables
At the first box look for PATH, double click it and press ADD. Add both C:\Program Files\Python37 and C:\Program Files\Python37\Scripts inside.
Now try again
When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)
This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
This is a really annoying problem. I've prowled the web for solutions, but all I found was tips about changing the PATH variable, which I did, of course. My Python install directory is C:\Python27. It' a 32 bit version. Whenever I type python in the command prompt, it says that it isn't recognised as an internal or external command. Currently, my PATH variable is set to C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts. Anyone has any ideas? I run Windows 7 by the way (64 bit). I'm pretty desperate. Heck, if nothing works I guess I'll try dual-booting Linux and Windows 7...
Just adding the Python path and trying again worked for me (without reboot).
MS-dos command for Python 2.7:
set PATH=%PATH%;C:\python27\
MS-dos command for Python 3.3:
set PATH=%PATH%;C:\python33\
(check if that is the folder where you installed Python).
Quick fix: May not be the most elegant or long term fix but if you are really frustrated and just want to get it to run, just copy paste the python.exe file to your current directory. This worked for me.
After adding the python folder to the system PATH variable, you should reboot your computer.
Another simple solution is: create a shortcut of the python.exe executable (probably it is in C:\Python27\python.exe, or similar) in a place like C:\Windows\system32 (that is, a place that already is listed in the PATH variable). The name of your shortcut should be python (maybe python.exe should work too). I mean, it can't be python - shortcut or similar, for your purposes.
To see the contents of the PATH variable, go to the cmd and enter set PATH.
After changing the PATH variable in windows, you need to reboot your system before it takes effect.
Edit: As stated by #tdelaney, only a restart of cmd.exe should be required. This is true atleast for Windows 7 64bit.
In your PATH have you got a leading space before your Python path?
For example:
"C:\somedirectory\bin; C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts"
If you have a leading space between path's "ry\bin; C:\Pyt", it won't work and is usually the cause for this type of issue.
When installing, there is a checkbox that is by default not selected, but it asks to add python to the environment variable. Re-install and check that box.
I'd rather the installer do it than struggle in the weeds myself.
Okay, as you said your Python install directory is C:\Python27, open my computer, then open c: drive,
if you don't see "Python27" named folder there then try to search it using search option, (in my case i found it in old.window folder, don't know how it moved there) cut and past it in c drive along with folders like, program files, user etc... , now open cmd and type python and hit enter to check if it is working now,
This is only a partial answer, but I found (repeatedly) that I'd have similar issues when I would use the gui installer and not go through the custom setup.
Using the custom setup option, then using the same settings, the "install for all users" (that then installs to C://python.version/blah instead of the user based default structure) WOULD allow the installer to setup PATH correctly.
This question already has answers here:
Trying to use py2exe, but python is not recognized
(2 answers)
Closed 5 years ago.
I tried all the steps in this tutorial.
Yet I keep geting this ouput
'python' is not recognized as an internal or external command operable program or batch file.
when I run
python setup.py py2exe
in command prompt
What am I doing wrong?
python.exe is missing in your path. Try to reinstall Python with admin rights or manually add C:\PythonXX\ to your path.
You can also just type out the full path to Python in the command prompt:
c:\pythonXX\python.exe setup.py py2exe
This was happening to e and it was making me soo mad
what i did was go to Start>run and run C:\Python27\python setup.py py2exe
and if your setup.py is in your python directory it should work
the above answer should work but its sometimes especially on a computer with locked down privileges that don't let you mess with path to just use a batch script or even a gui such as GUI2EXE
Assuming you're on Windows, go to:
start > control panel > system and security > system >advanced system settings
Now click environment variables, then the new button under system variables. Set the variable name to PATH(yes, caps. No quotes) and the value to the path of the Python folder under which Python is installed. For me, for example, it is C:\python27.
However, if the installation was user specific it will look more like C:\USER\python27. USER is, of course, filled in with your user account's name.
The number after the name python as the path value will be determined by the version of Python you have.