bit of background. I work for a VFX studio and we have in the past had to alter .py files for other programs just using notepad or notepad++, but no one ever actually used python.
So I have been rolling out python automation scripts now and they're working great, except one problem. Any machine who previously had python scripts associated with something other then python fail.
The script is called with an argument:
myScript.py <argument>
then I use:
print sys.argv
versionName = sys.argv[1]
This works great on all machines that never had python files associated with anything, however machines that had python previously associated with another application fail
it won't read the argument and i get a list index out or range error.
The print line shows it is not recieving the input properly either.
Any thoughts on how to fix this?
Edit: Script returns this when run:
z:\pythonScripts>Make_version_1.py test
['Z:\\pythonscripts\\Make_Version_1.py']
Traceback (most recent call last):
File "Z:\PythonScripts\Make_version_1.py", line 20, in <module>
versionName = sys.argv[1]
IndexError: list Out of range
This error is not returned form the majority of the machines in the office .. just ones where .py files had been associated with another program before python 2.7.6 was installed, so i know the code works.
You need to tell Windows that you want to pass arguments to python. Open a command prompt, and do this:
assoc .py=PythonFile
ftype PythonFile=python.exe %1 %*
Just wanted to share that i fixed the issue. Apparently preassociating a .py file breaks the setup of the windows path during installation. On my main machine i didn't need to adjust it, but i now have on all machines by adding ;c:\python27;c:\python27\scripts to the environment variable. Now it works fine
Related
Let's say I have a python file named myfile.py in a certain directory.
How do I just call the python file directly without invoking python.
myfile.py
and not
python myfile.py
Edit:
To be more precise.
just typing the filename in the command line will not work.
Typing start program.py however should work.
Windows normally has information telling it which program is the default program for a given suffix.
Concerning #go2nirvan's comment: Though windows is not an oracle, it can have information for each file suffix to know what's the related default application.
Even many linux desktop application associate a default application to certain mime types.
If you click on .xls (depending on what is installed) either Excel, or OpenOfficeCalc or LibreOffice will be opened)
Windows associates file suffixes to file types and file types to applications, that are supposed to start it.
If you open a CMD window and you type
assoc .py
You should get an output similar to: (I don't have a windows machine nearby, so can't tell you the exact output)
.py=Python.File
Then type
ftype Python.File or whatever the previous command gave you and you should see which executable shall be used.
This should be something like
c:\System32\py.exe
which is a wrapper program, calling the real python executable according to some rules
If this doesn't work, then please tell which version of python you installed and how you installed it (for all users, for current user, ...)
From command line you have to call (If I recall correctly)
start test.py and it will execute the file with the associated executable
Windows
Set up Python on Windows to not type "python" in cmd
Linux
https://superuser.com/questions/828737/run-python-scripts-without-explicitly-invoking-python
I'm on day two of a desperate attempt to run a Windows executable from a Python script. This .exe file is opaque to me; in other words, I don't know how to open it up to look at the source code. But importantly, this executable needs to be located in the same directory as a csv file full of parameters in order to work.
When I call the file directly in the command line:
C:\Users\pb\nw_river> Run_Model.exe
the executable works exactly as expected.
However, when I try to call the same file from some python code:
import subprocess
subprocess.Popen(["C:\Users\pb\nw_river\Run_Model.exe"], shell=True)
I receive error messages that indicate that the executable can't "see" the required csv file.
I don't have any of these problems when I run the script on my macbook (MacOS Catalina 10.15.3), but I really need to get this to run on my work computer, which has a Windows 10 Pro OS.
I'm a total noob to Windows. Has anyone ever experienced this issue, and do you have any advice? Thanks in advance!
I've just started learning Python using Learning Python by Mark Luts. In his book he offers an example of a simple script that is called through the Windows shell. In the example, he calls is as follows:
C:\code> python script1.py
I've gone and modified the Environment Variables on my machine so that I can call
C:\User\Example> python
to open up the interpreter and I can also call something like
C:\User\Example> script1
to run a script that I've written and placed in my desired directory. My issue is that I can not call
C:\User\Example> python script1.py
in my command line the same way he does in the book. He's mentioned something about a PYTHONPATH Environment Variable, however, this variable isn't present on my machine. I only have 'path', 'TEMP', and 'TMP'. Particulary, when I try to make such a call I get the error
python: can't open file 'script1.py': [Errno 2] No such file or directory
What do I have to do in order to get this sort of command to work properly on the command line?
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
#echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
You are calling python from within the context of C:\User\Example, and passing it a name of a file you want to run through the intepreter (script1.py). It is clear that the PATH variable is setup correctly such that you can call python from anywhere on you computer, since we can see that it is running but not actually able to find your script.
However, you stated in the comment that your scripts are actually located in C:\User\Example\my_scripts. In other words, you are passing python a name of a file that doesn't exist!! (at least from the contect of C:\User\Example).
You need to be in the directory of the script in order for the python executable to be able to find it.
Alternatively, you can run the python command and give it more information as to where the script is. For instance, you could run python .\my_scripts\script1.py if you are running from within the contect of C:\User\Example and your scripts are in C:\User\Example\my_scripts.
so my ultimate goal is to use python to read a specific application's windows event log when triggered by a file update.
Here is my problem, python I believe does not have access to the event logs stored in C:\Windows\System32\winevt\Logs. Whenever I try to read the files I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
I tried every form of escaping, string split/join and using quotes on the file path and I always get the same error. I even cheaply used the os.system('dir "C:\Windows\System32..."') command in the python command prompt to list directories higher in the path for the log to verify access and I receive similar errors up to the C:\Windows\System32 directory, that one will list just fine.
Example:
C:\Windows\System32\winevt\Logs - File not found
C:\Windows\System32\winevt - File not found
C:\Windows\System32 - Lists files
I know these directories exist. So from here I figured I could use a bash script to copy the event log into a temp folder for python to read. I wrote a real simple bash script containing:
xcopy /Y "C:\Windows\System32\winevt\Logs\XXXXXXX" c:\Temp
(XXXXXXX) being the name of the log I want to copy for the python script.
The bash script runs fine on its own but when called by my python script it fails, refuses to copy the file because it can't find it. I have even tried using py2exe to create an exe to then run in administrator mode. This failed too. With similar errors of the file not found. I'm not sure I understand the permissions python uses to call the script and why the bash script cannot copy the file even when it can do it in a normal command prompt.
Any suggestions or flaws you can point out in my logic would be great. I am very stuck.
You are using a 32bits python on a 64bit install of windows.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
You can use os.listdir("c:\windows\sysnative\winevt\logs") as a workaround to read from the real system32 dir from a 32bit python interpretter runing on a 64bit windows...
Sources:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
http://support.microsoft.com/kb/942589
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave