CMD can't normaly read 'C:\Program Files' - python

I have some problem with my WIndows CMD.
Some time I need to open python file using CMD command. And I write: 'C:\Program Files\Python X.X\python.exe file.py' but have error: 'C:\Program' isn't system command (maybe not the same, I have another OS language).
With different methods I have different errors but can't open python file.
Examples:
(Picture) translate: can't find 'C:\Program'...
(Picture) another example when I trying to write python directory first and then start python file, but it can't find python file.
Thanks for helping me.

There seems to be 2 different problems here.
Windows does not recognise spaces in directory or file names on the command line, so you need to put the directory insied "" .
i.e. "C:\Program Files\Python 3.4\python.exe"
In your second picture, suggests that run.py does not exist in the current directory. Change Directory to where the run.py file is before running that command.

First of all go to the directory where your python file is located ... like:
cd "c:\users\someone\documents\..."
On your pictures you are trying to run python file located in system32 folder but i guess it is not located there so move where the file is with that cd command
Then as Martin says the problem with path of python.exe is the space between words. To solve put the path into quotation marks.
But u can add python to system path and insted of writing full path u can write only
python file.py
How to add python to path see here https://superuser.com/questions/143119/how-to-add-python-to-the-windows-path

Related

Run Python Script From Script Directory/Current Directory

[Introduction]
Hi! I'm working on a python script to automate installation of UWP-Apps, it's been a long time i'm not touching Python; until this day. The script uses Depedencies inside the script directory, i've looking up on my older scripts and found this specific code.
os.chdir(os.path.dirname(sys.argv[0]))
[Problematic]
However, using the above code doesn't work on my current script but it's working fine on older scripts. When using above, it shows:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
Already looking up on Internet about this topic; but most of them was talking about running the script from outer/different directory that leads me to dead end.
Any helps is appreciated :)
The easiest answer is probably to change your working directory, then call the .py file from where it is:
cd path/to/python/file && python ../.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runPython.sh in the directory where you're running the python script from, is:
#!/bin/sh
cd path/to/python/file
python ../script.py
Make it executable (for yourself):
chmod +x ./runPython.sh
Then you can simply enter your directory and run it:
./runPython.sh
If you want to only make changes to the python script:
mydir = os.getcwd() # would be the folder where you're running the file
mydir_tmp = "path/to/python/file" # get the path
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd()
The reason you got an error was because sys.argv[0] is the name of the file itself, instead you can pass the directory as a string and use sys.argv[1] instead.
import os
from os.path import abspath, dirname
os.chdir(dirname(abspath(__file__)))
You can use dirname(abspath(__file__))) to get the parent directory of the python script and os.chdir into it to make the script run in the directory where it is located.

No such file or directory on Mac terminal for python

hello.py is my first python program. It is saved on my desktop.
In the terminal I write in front of
user#AA-MacBook-Air ~ % python3 hello.py
The error is
can't open file 'hello.py': [Errno 2] No such file or directory
Kindly help me understand the problem and solve it.
In the terminal you are currently in the directory ~. This signifies the folder /Users/<username>. Your script is on your desktop.
Type cd Desktop to change to /Users/<username>/Desktop and then run python3 hello.py.
you first need to change destination with cd
The error message, No such file or directory pretty much gives the explanation. Check if the file hello.py is present in the correct working directory. This can done graphically or using the ls command. If it is not present, copy the file the to the directory or navigate to the location of the file hello.py in terminal using cd.

Running a python script from command prompt

I'm working on a tutorial for a big data class and am having trouble in the command line (Windows 7 Pro). I'm not very familiar with the command line environment so this is probably something simple, but here goes:
I have a python script called mapper.py that is stored in the directory
E:\Documents\School\Math\M 461\MapReduce\PythonScripts
and a file named
Medicare_Provider_Util_Payment_PUF_CY2013.txt
that is stored in
E:\Documents\School\Math\M 461\MapReduce\Data
Python (Anaconda) is installed at C:\Program Files\Anaconda3. I'm trying to feed the file to the script and execute it from the command line using
type Medicare_Provider_Util_Payment_PUF_CY2013.txt
| 'C:\Program Files\Anaconda3\python' mapper.py
I use the apostrophes because otherwise it doesn't like the space in Program Files. However, when I execute this command it says that "The filename, directory name, or volume label syntax is incorrect." I'm not sure where to go from here so any guidance would be appreciated.
The text file and the Python file aren't in the same directory, so your example can't work. Try this series of commands in at your command prompt:
First, make the common parent the current directory
E:
cd E:\Documents\School\Math\M 461\MapReduce
Then run your script, giving the path to the data and python files:
type Data\Medicare_Provider_Util_Payment_PUF_CY2013.txt | 'C:\Program Files\Anaconda3\python' PythonScripts\mapper.py

PYTHONPATH: command line calls on Windows 7

There are python scripts with command line arguments that I'd like to call from any location on my PC.
The idea is to share the corresponding package with others so they can open up a CMD window and run
python thescript.py arg1 arg2
regardless of their location.
How do I setup the python path/ PATH environment variables?
I've setup a package in site-packages, added that path to $PATH and edited PYTHONPATH to include the module directory (which includes __init__.py), but CMD won't find the relevant scripts.
python: can't open file 'thescript.py': [Errno 2] No such file or directory
Thanks.
Python does not look up scripts on some sort of path.
You have 2 options:
Use the full path:
python /path/to/thescript.py
Place the script in a directory that is on your PATH, make it executable (chmod +x thescript.py) and give it a Shebang line:
#!/bin/env python
The second option is probably preferable. On Windows, you can install pylauncher to support shebang lines; if you use Python 3.3 or newer, it is already included with your Python installation.
Another option would be to create a batch file for each script you care about, and put the batch file somewhere in your PATH, e.g. create a file called thescript.bat containing...
#echo off
the\path\to\python.exe the\path\to\thescript.py %*
...then you can just run...
thescript arg1 arg2
...which is about as terse a syntax as possible.

Why is neither the executable nor its launcher script found despite being added to PATH

I followed the instruction to install pygmentize in order to use syntax highlight in Latex environment, however TexMaker always produces an error says pygmentize is not found. I then tested to run the .cmd file itself using command prompt and the system says:
C:\Python27\Scripts>pygmentize.cmd
C:\Python27\python.exe: can't open file 'C:\Python27\Scripts\pygmentize': [Errno
2] No such file or directory
I added path variable for both directories: C:\Python27 and C:\Python27\Scripts. Within C:\Python27\Scripts, there are two files:
pygmentize.exe
pygmentize.cmd
Where the content of pygmentize.cmd is as follows:
#echo off
set PYTHONPATH=C:\Python27
%PYTHONPATH%\python.exe %PYTHONPATH%\Scripts\pygmentize %*
The original instruction doesn't contain a \ after %PYTHONPATH%, I added because the I realize the error message is kinda odd when it says C:\Python27Scripts\python.exe.
So my question is, why doesn't the Tex system recognize the executable pygmentize? Following an instruction from a SO from https://tex.stackexchange.com/questions/23458/how-to-install-syntax-highlight-package-minted-on-windows-7, I added a line:
\newcommand\TestAppExists[3]{#2}
in my .tex file and it worked only under Ubuntu. So what's the purpose of the .cmd file in this case?
cmd.exe will prefer .exe to .cmd when choosing which pygmentize to execute, all else begin equal. python.exe will perform no magic at all and will not know what to make of C:\Python27\Scripts\pygmentize unless a file with that exact name exists.

Categories