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!
Related
how to solve error The directory name is invalid in cmd while operating with pipenv
here is the code-
C:\Users\Admin>cd C:\Users\Admin\PycharmProject\sound in python\sound.py
The directory name is invalid
I've tried putting this directory into path of environment variable but it didn't worked what to do please explain in simple language
You are trying to change directory to a file while you should be changing to a folder.
So you should try this:
cd C:\Users\Admin\PycharmProject\sound in python
I just try to create some new folders with Python's (3.7.3) os.makedirs() and os mkdir().
Apparently, it works fine because no error occurs (Win 10 Home). But as I try to find the created folder in the windows explorer it isn't there.
Trying to create it again with python I get an error:
'[WinError 183] Cannot create a file when that file already exists:'
Strange thing is, all this worked fine on my computer at work (Wind 10 too), and also on my android tablet.
I've already tried to use relative & absolute paths in makedirs / mkdir.
Ok, it's all about these few lines:
import os
# print(os.getcwd()) shows: C:\Users\Andrej\Desktop
# tried relative path..
os.makedirs('Level1/Level2/Level3')
# tried out some absolute paths like...
os.makedirs('C:/Users/Andrej/Desktop/Level1/Level2/Level3')
os.makedirs('C:\\Users\\Andrej\\Desktop\\Level1\\Level2\\Level3')
UPDATE: It works perfectly when I write makedirs command directly in the Windows powershell. The issue above only occurs when I write this code in Visual Code Studio and then start the file from the powershell by typing in: python makedirs.py...
I just had the same thing happen to me, except I was creating a folder in the AppData/Roaming directory and was using the version of Python from the Microsoft Store.
Apparently, files and directories created in AppData using that version of Python, will instead be created in:
%LocalAppData%\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache
I found what causes this problem finally!
All of the created test folders have been moved to a folder named VTROOT. This folder is created by Comodo Internet Security I use...Its "Auto-Containment" moved all folders I set up by running the code from .py-file (only then) in the PowerShell.
Disabling the auto-containment of Comodo solves the problem..Oh my..
Thank you anyways!
the "if" part will check if the file exists already and consequentially solve the ERROR: '[WinError 183] Cannot create a file when that file already exists:'
the "import sys" part will fix the issue with windows where the folder is created but not visible by the user (even with show hidden files turned on)
import sys
import os
path = 'your path'
def make_dir():
if not os.path.exists(Path):
os.makedirs(Path)
or
import sys
import os
path = 'your path'
def make_dir():
mode = 0o666 #read and write for all users and groups
# form more permission codes search for: *chmod number codes*
if not os.path.exists(Path):
os.mkdir(Path, mode)
Try adding the below import statement:
import sys
This should work.
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
I am using python 2.7.11 and I have downloaded setuptools-2.2 for its included setup.py file. Then I did a python setup.py install (This command is successful), and I added the path for this setup.py file to ~/.bash_profile.
export PATH=/opt/python2711/lib/python2.7/site-packages/:$PATH
export PATH=/path-to-setuptools/setuptools-2.2/:$PATH
However, when I tried to install another software (./pybombs install uhd), my computer kept complaining:
Build failed. Re-trying with reduced makewidth and higher verbosity.
python: can't open file 'setup.py': [Errno 2] No such file or directory
I actually tried to add the path of that egg file as well, but it couldn't help either. I don't know much about this setup.py file. Could somebody point out how to fix this compiling error?
Thanks in advance!
add your path in ~/.profile
export PATH=$PATH:/path/to/dir
~/.profile: executed by the command interpreter for login shells.
how ever the path variable is in
/etc/environment : file open and add the path here you should log in as root
I just installed ubuntu today, and I can't figure out how to run my .py file.
I opened terminal and typed in python test.py and I receive an error saying "python: can't open file 'test.py': [Errno 2] No such file or directory"
If you could please help me out, that would be great.
Thanks
You are using the correct command to open it, but there is no test.py file in your current working directory.
Try using
ls
To verify what files are in fact in your working directory and use
cd path/goes/here
To change directories to where the file is located.
Then try python test.py
python test.py would try to execute the test.py from the current directory.
else try giving the complete path of the test.py
like "python /usr/local/bin/test.py"