how do i get windows to execute .py with specific `python.exe`? - python

i tried in Control Panel\Programs\Default Programs\Set Associations, and also assoc/ftype, but windows keeps using the wrong python.exe (C:\Python27\) instead of the one i want (C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\)
>where python
C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\python.exe
C:\Python27\python.exe
C:\Program Files (x86)\LilyPond\usr\bin\python.exe
>assoc .py
.py=Python.File.WinPython
>ftype Python.File.WinPython
Python.File.WinPython=C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7
.9.amd64\python.exe "%1" %*
>set PATH
Path=C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\python-2.7.9.amd64;...C:\Python27;C:\Python27\Scripts;...C:\Program Files (x86)\LilyPond\usr\bin;...
PATHEXT=.PY;...
i just made up Python.File.WinPython, is that allowed?

https://github.com/winpython/winpython/wiki/Installation#Registration
(but you will loose your python2.7 registration if you do that)
the other solution is :
call C:\Users\nlab\Downloads\WinPython-64bit-2.7.9.5\scripts\env.bat
python my_progam_.py

Related

Running cpplint on all .cpp files

I want to run cpplint.py on all my .cpp files in a folder in Windows CMD.
py .\cpplint.py *.cpp
This somehow doesn't work. I get the Error:
Skipping input '*.cpp': Can't open for reading
Total errors found: 0
I thought * would be the operator for select all, am I wrong?
P.S.: There's a similar post, but it didn't really help.
Windows' CMD doesn't do wildcard expansion: https://superuser.com/questions/460598/is-there-any-way-to-get-the-windows-cmd-shell-to-expand-wildcard-paths
It's up to the application to handle the expansion of * into paths on Windows, which is in stark contrast to Linux, where the shell itself handles that. So you're passing the string-literal * to cpplint.py, which is not a file.
In a batch file or from the command line you can do:
for %%f in (*.cpp) do py .\cpplint.py %%f

Executable file in venv\Scripts not recognised as a command

I have cloned a github repository - pypastry - onto my Windows machine, and run pip install -e pypastry to install it in my virtual environment, so the project structure is like this:
│-- pypastry
|-- venv
|-- Scripts
|-- pastry
The executable file, called 'pastry', is sitting in venv\Scripts as I would expect, but Command Prompt isn't recognising it, and I am receiving the error:
'pastry' is not recognized as an internal or external command, operable program or batch file
I don't think it's a problem with PATH as the first item in PATH is C:\Users\User\Documents\pypastry\venv\Scripts.
This is the executable file itself:
#!c:\users\user\documents\pypastry\venv\scripts\python.exe
# EASY-INSTALL-DEV-SCRIPT: 'pypastry==0.0.1','pastry'
__requires__ = 'pypastry==0.0.1'
__import__('pkg_resources').require('pypastry==0.0.1')
__file__ = 'C:\\Users\\User\\Documents\\pypastry\\pypastry\\pastry'
with open(__file__) as f:
exec(compile(f.read(), __file__, 'exec'))
Can anyone spot anything in this file which might mean it's not recognised as an executable on Windows or think of anything else that could be going wrong?
EDIT TO ADD:
If I run the file with the explicit python invocation (python pypastry\pastry) it works, but I don't want to have to do this as it creates other issues.
Windows is telling you that it does not find an executable called "pastry" (pastry.exe, pastry.com, pastry.bat, pastry.cmd ...) on your PATH.
But pastry.py is. Isn't it? The problem is that your windows is not configured to recognize .py as an executable extension.
To run python scripts in Windows without the explicit invocation of python.exe, you need first to have the .py extension associated to the executable python. And then the .py extension added as an executable extension.
You do so in three steps:
Associate the .py extension to a PythonFile file type
ASSOC .py=PythonFile
Associate the PythonFile to your python.exe executable
FTYPE PythonFile="c:\users\user\documents\pypastry\venv\scripts\python.exe" "%1" %*
Associate the .py file extension as one of the default executable extensions
SET PATHEXT=.py;%PATHEXT%

Python can't run absolute script?

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

python rename script not working when executed from finder (Mac)

Helly everyone,
I'm kind of new to python, but I have written a little script that helps me rename file extensions.
It's very short, so I'll post it here:
import os
for filename in os.listdir("."):
if filename.endswith("gd"):
os.rename(filename, filename + ".dat")
Now, as I have several directories I need to execute this in, it was getting kind of annoying to always navigate to the folder in terminal and execute it from there using the commmand
python renamescrip.py
Now I stumbled across some advice that would make a python script execute upon double clicking in the Finder, which would make life a lot easier for me, as I would just need to copy and paste the renamescript.py into the folder in which I want to rename the files and double click on it to execute it.
I followed these instructions: http://skillfulness.blogspot.de/2010/12/how-to-run-python-script-from-mac-os-x.html , which show how to create a .command file.
However, when I double clicked the renamescript.command file, it didn't change the file names in the directory.
I also tried opening the .py file with Python Launcher, but that also didn't change the filenames.
Both times a terminal window opened, though, with the message:
cd '/path to directory with files to rename/' && '/usr/local/bin/pythonw' '/path to directory with files to rename/renamescript.py' && echo Exit status: $? && exit 1
Exit status: 0
logout
or if I click on the .command file it's just:
/path to directory with files to rename/renamescript.command ; exit;
logout
What am I doing wrong?
When you double click, you are not running with the same directory form your current directory as when you run from the shell. This causes your os.listdir(".") to not find the files you think it should. You should change your directory at the beginning of your script with
os.chdir(os.path.dirname(os.path.abspath(__file__)))
before your os.listdir call

cannot run python script file using windows prompt

I am trying to run a python script from the windows command prompt, but I receive the following error message:
"python: can't open file 'pacman.py': [Errno 2] No such file or directory"
when I try the command:
c:\Program Files (x86)\Python27>python pacman.py
This particular python script file pacman.py is located in the following folder:
C:\Users\Chris\Dropbox\edX\CS188x\search
So I added this folder to PYTHONPATH and confirmed that is was there using the following code:
>>> import sys
>>> sys.path
['', 'C:\\Program Files (x86)\\Python27\\Lib\\idlelib', 'C:\\Users\\Chris\\Dropbox\\edX\\CS188x\\search', 'C:\\windows\\syste...
I also checked the permissions on this file:
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.W_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.R_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.X_OK)
True
So I am really not sure why I can't run this file, even though its path has been added to PYTHONPATH. Any help would be greatly appreciated. Thank you.
PYTHONPATH is used by the python interpreter. It is not the same as Windows' PATH environment variable. You can't use it as a search path for passing files to the interpreter on the command line.
So, you need to specify a valid path to the file. Either by using he same command as you've been trying with the difference being your current directory is the same as the location of pacman.py, or by specifying the full path to the file.
did you try running the script from its directory?
i can only guess, but maybe its some issue with the file being located inside your dropbox folder...
python "C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py"
or
cd C:\Users\Chris\Dropbox\edX\CS188x\search\
"c:\prorgam files (x86)\python27\python" pacman.py
SOLVED! So the comments were right in that I had to change the directory to the location of the file, but what was missing was that I had to edit the system environment variable PATH to include the location of python.exe, which is my case was C:\program files (x86)\python27 but for most people is just C:\python27. Thanks everyone for all your help!

Categories

Resources