I am running Windows 10. I have recently changed location to my python folder and interpreter.
Unfortunately, when i now run python scripts in command prompt, I always have to type "python" before the name of the script I want to run.
Running the script directly - for example: C:\Python 37\mypythonscript.py -generates absolutely no reaction from the command prompt. I simply get back directly to C:\Python37>
If I however run C:\Python 37\python mypythonscript.py, the script is properly executed.
I have changed all the file associations using "ftype" and "assoc" in the command prompt. When I start command prompt and type "python", the python compiler correctly starts.
My problem is that I always have to type "python" before the name of my script for the command prompt to recognize that it needs to execute python.
I would like to open command, change directory to the directory with my python scripts, and simply run the python scripts by running the name of the scripts (mypythonscript.py) without having to type "python" in front of it.
Could you please help me identify what is wrong? How can I once again run python scripts in the command prompt without having to type "python" in front of the script's name?
Thank you in advance.
If you use a python 3 Virtualenv in powershell the above problem can be very confusing. Typically "py .\yourscript.py" or "py .\yourscript" will work but ".\yourscript.py" or ".\yourscript" will fail and might return a "ModuleNotFoundError: No module named '<module>' " error.
This happens because your .py association is not correct as described by Eryk Sun in the above second remark. The "py launcher" he names is, in most cases, "C:\Windows\py.exe". I my case it was already installed and I just needed to adjust the .py extension as described by him. It can also be done via the "Choose default apps by file type" settings.
A useful help is to define "$env:PYLAUNCH_DEBUG=1" in your powershell. After that "py .\yourscript.py" will have extra output showing where your python executable is found.
If there is no extra output it means, as before, that you don't have ".py" associated with "py.exe". If you start a "cmd" and "ftype python.file" looks ok, you need to read Eryk Sun's first remark :-)
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 have been using Jenkins for a few years and recently bought a new Windows 10 PC. I installed Jenkins 2.89.2, Visual Studio 2017 and Python 3.6 and copied several Jenkins jobs from my previous Windows 7 PC.
The problem that I encountered was that all the python scripts in the free-style Jenkins jobs now do nothing.
I have similar command-line batch files which run these python scripts which work just fine in a command window on the new PC.
I have also checked the file associations with ftype, and ended up changing it:
ftype Python.File
Python.File="D:\Python36_64\python.exe" "%L" %*
My work-around is like this:
Example line which worked under Windows 7:
CreateBuildNumber.py <= uses PATH to find this file, then file associations to run python
Replacement line need to work under Windows 10:
python .\Scripts\CreateBuildNumber.py <= uses PATH to find python.
How can I avoid explicit paths in my scripts?
Update:
D:\project>assoc | findstr -i python
.py=Python.File
.pyc=Python.CompiledFile
.pyd=Python.Extension
.pyo=Python.CompiledFile
.pyw=Python.NoConFile
.pyz=Python.ArchiveFile
.pyzw=Python.NoConArchiveFile
echo %PATH%
D:\Python36_64;D:\Python36_64\Scripts;.\Scripts;"C:\Program Files\CppCheck";C:\windows\system32
Further Info
I removed .\Scripts from the %PATH% and re-ran the job, having also moved demo.py into .\Scripts, now instead of doing nothing there is the error:
'demo.py' is not recognized as an internal or external command, operable program or batch file.
This means that Windows IS looking for and finding python scripts, but is failing to execute them. Why would windows not take the next step and use file associations to find an executable program to run, taking the file as a parameter?
Update:
Now everything works as it should and I don't know why.
I recently started using a different job on Jenkins that I had neither run nor touched for over two years and that one just worked without modification. I have since gone back over the job in question and reverted all the changes and this one works as well.
My guess is that Windows 10 has been patched.
The fact that "demo.py" gives the message '...is not recognized as an internal or external command' doesn't convince me that your script is recognized as executable. If I type 'turkey.abc' into a command prompt window I get the same error, and I don't have a tool for executing '.abc' files.
I see two possibilities here:
1) In batch scripts, executable extensions sometimes must appear in an environment variable called PATHEXT.
In the Jenkins batch script, add a "set" command near the top of the script to dump the environment variables for your running script into your Jenkins build log (you can remove the set command after this is debugged). Run the build. Look not only for the definition of PATH, but also at PATHEXT. Is ".py" one of the extensions listed there?
I have experienced this problem with Perl scripts. However, I'm wimping out on claiming this definitely since in testing on my Windows 10 home PC I am successfully executing .py scripts even without it being in PATHEXT, so it's something to try but it may not be this.
2) Another possibility is that the environment in which your service is running is different than the environment you get when you open a command prompt on your desktop (because the Jenkins service runs as a different user than the one you log in as.)
Adding "set" to your Jenkins batch commands will help debugging this too, since it will show you the environment your Jenkins script is running in. Then you can examine PATH to see if your script folder is being found.
It is also possible that the file associations for Python were installed for your user only, not for all users (i.e., in HKEY_CURRENT_USER in the registry instead of HKEY_LOCAL_MACHINE). That is harder to dump into your Jenkins log - the 'reg' command would do it, but it will take you a number of tries to get everything you need. You might be able to figure it out by just examining the registry. Search for ".py" - if it occurs in HKEY_LOCAL_MACHINE that is not it; if it occurs in HKEY_CURRENT_USER that is at least part of the problem.
I don't know if this will fix your issue but you shouldn't have a relative path in your PATH environment variable.
Can you try again after having removing .\Scripts from the PATH variable? (don't forget to open a fresh new terminal do get the new %PATH% value)
The problem is that your PATH variable doesn't include the paths of you python scripts. You need the full path of the Scripts directory. Not the relative path .\Scripts.
I am trying to access Python through a Windows command prompt, but am at an office and cannot add an Environment Variable. So, I cant follow the advice provided here. Thus, I added a User Environment Variable through User Accounts and use Variable:python and Value: C:\Python33. When I do a Windows Run check on %python% I reach the required Python33 folder. However, when I type python -Version in the command prompt I get the error 'python' is not recognized as an internal or external command, operable program or batch file. I am not sure how to proceed.
Run Python from the command prompt and include the full path to python.exe. For example to get the version:
C:\Python33\python.exe --version
When you run a script you specify the path to the script if you're not in the directory where the script is located. For example, if your script is in C:\scripts and is called my_script.py:
C:\Python33\python.exe C:\scripts\my_script.py
Instead of setting environment variables, you can use the whole path, like Neo wrote e.g.:
C:\Python33\python.exe yourPythonScript.py
Remove any environment variables you have added.
C:\Python33\python.exe pyscript.py should work.
I once Had a similar problem, but i couldnt find any solution, not even adding the path to the python folder was working.
Then I opened Python and clicked in File, then browser path, and I found that I had another folder that contained the real Python folder, not the default one. Maybe it could work for you too.
Hi I noticed that whenever from the command line (using windows 8.1) I type
python file.py
It automatically knows that I meant to write python.exe file.py
How does it do this?
I installed Anaconda, and I understood I have an environment variable pointing to python.exe. But that doesn't explain why I need not type python.exe everytime.
This is not a python feature. The behavior to call executables without file extension is defined by the operation system and the PATH variable. Wikipedia has a good answer to your question
PATH (variable)
...
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given.
...
The file name after the executable could be everything. So if you want you can call python demo.txt. If the file content is readable for python it would also be executed.
Python is searched for using the path. Find the PATH in the list of environment variables.
If you do not write python and simply double-clicks the file, the registry is searched.
You can see what and associate files with program using ftype and assoc from the command line. See e.g. http://www.fileformat.info/tip/microsoft/assocftype.htm
If you omit the extension the registry is also searched. This cmd shell searches the environment variable PATHEXT and the registry for finding python.exe. After this, it is using the registry to find the location.
When you register your python distribution from within Spyder, these changes are made.
You can reveal this information by using ftype and assoc like I wrote in the beginning.
I'm learning to program Python from the "Learning Python the Hard Way". On one of the chapters it tells me to use pydoc command. The windows 7 cmd failed miserably with that command.
When I typed in pydoc raw_input() on cmd following error codes were generated:
'pydoc' is not recognized as internal or external command,
operable program or batch file.
I've looked back previous two questions on same issue:
Pydoc is not working (Windows XP)
How to get pydoc command working in Windows?
So far I have created a pydoc.bat with following line
#python c:\Python26\lib\pydoc.py %*
and saved it in C:\python27\Tools\Scripts.
I've also tried to change the PATH to C:\python27\Lib. I called the two variables python and pydoc because I'm not really sure I set the PATH correctly.
It's still not working.
What did I do wrong?
To get help on python when in the PowerShell window, type:
python -m pydoc raw_input
substituting your function name for raw_input.
Hey I know this post is a bit old, but I wanted to let you (and anyone else out there) know about this:
C:\Python27\Tools\scripts\pydocgui.py
It sets up a webserver on localhost:7464. Simply visit the address in your browser and you have access to pydoc :) Hope this helps someone
You added this to your script file:
#python c:\Python26\lib\pydoc.py %*
But it seems that you are using python 2.7
I run a Windows 7.
I am using the command prompt, cmd.
I am learning Python from Learning Python the Hard Way.
I was having the same problem as you, and attempted to solve it on my own from what you gave me.
Here is what I got:
On gedit, I typed
#python C:\Python27\Lib\pydoc.py %*
It is important to note two parts:
The C:\ is capitalized, the L in Lib is capitalized.
Both of those were left under-cased, and that created a syntax error when I tried to run it.
I saved this as
pydoc.bat
from gedit in C:\Python27\Tools\Scripts.
From cmd, I cd'd into the above address. From C:\Python27\Tools\Scripts I typed pydoc.bat open
and it worked. It showed me the information I needed.
The only difference between what you did and what I did seems to be the capitalization of certain letters.
Check your typing and ensure it fits what the computer needs to see.
Copy paste the bat file in this path.
C:\python27
and then run it as :
pydoc.bat raw_input
this should work. make sure that the PATH is set to:
C:\python27
This will help you to run the bat file from anywhere in the cmd line
set the path as
C:\Python27>python (or wherever you installed python)
Then write command -m pydoc raw_input
which will look as
C:\Python27>python -m pydoc raw_input
Note: There's a space between python and -m
There are several ways to fix it:
Check if path you've added to PATH is in there.
Type in command line PATH. If path you've added isn't there then Restart Windows.
If it is go next line.
Type pydoc.py instead of pydoc
If you don't want type pydoc module extension every time, just add .PY extension
to PATHEXT variable located at the same place as PATH variable does
(Computer → Properties → Advanced system settings → Advanced → Environment Variables...
→ System Variables → PATHEXT). Restart your Windows after adding of extension.
Type Pydoc.py x replacing x with whatever document you want to read.
For example Pydoc.py raw_input (in cmd ofcourse)
Do not forget to change path to the lib directory. do it like this:
Right click on my computer>properties>Advanced tab>environment variables> search for the value PATH in system variables>edit> enter this -> ;C:/Python27/lib
Here's how you do it from zero/layman (my kind of) condition, learning python:
If you have a Win7 32bit system with no environment variable set manually after installing (basically nothing else done after just installing it) python 2.7 or any version lower than python 3 from website, then follow these steps:
1) Open Command Line, and type (in exact case):
cd\
until you come back to "C:\" directory (root/base directory where your python is installed).
2) Then type:
cd Python27
As my base dir or drive is C drive, my path is this, yours may be different
hence you type the route of Python27 (or 26 or the name of your python folder) folder where you have installed Python2.7 (or whatever version).
3) Then type:
cd Lib
You just entered the Library folder where pydoc.py file is present as per default installation from internet (for me it was the case).
4) Then simply type:
pydoc.py
You will get the required pydoc output which seems to be of the kinds of "man" page of PERL/Linux or the "Help" of C/Windows.
5) Then simply type:
pydoc.py raw_input
Name of the required command for which you want to see documentation.
Now you know how to do it for all the rest of the commands.
you must not be in correct directory in your command prompt while typing "pydoc raw_input"
first type:
cd\python27\lib
then type:
pydoc raw_input
I have the answer: see the thing is we been running pydoc raw_input in the cmd, but what we where forgetting is that to run python files we have to type .py at the end of the file
im using windows 7 NVidia, to run you got to be in it's Dir C:\Python27\Lib, then type pydoc.py and enter.
There you go hope this was helpful.