Opening a python file in terminal. (Ubuntu) - python

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"

Related

cd directory_name python python_file.py gives error in command line " the system cannot find the path specified"

I am running python file from command line. To be more specific my .py file is in location F:\new_github_code\cocktail_party\data\audio\audio_downloader.py>. I want to run audio_downloader.py file from the cocktail_party directory not from the audio directory.
I tried by using the following script from cmd. Any help will be appreciate
enter image description here
Try this. It will work:
python data\audio\audio_downloader.py

How to run a Python script in VSCode using the same path of the script? (otherwise it can't find an input file located in the same folder)

I have this Python code: https://github.com/andreagrandi/aoc_2019/tree/master/aoc
which runs perfectly from the terminal (Python 3 required) if I do for example: python aoc_03.py
But if I try to run it from VSCode, taking advantage of the Python extension and integration, I get this error:
(aoc) ➜ advent_of_code_2019 git:(master) /Users/andrea/.virtualenvs/aoc/bin/python /Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py
Traceback (most recent call last):
File "/Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py", line 70, in <module>
with open('aoc_03_input.txt', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aoc_03_input.txt'
My guess is that when invoked from VSCode, the script is run from a different path, so it cannot find the file aoc_03_input.txt which is located in the same folder of the script.
How do I tell VSCode to run my script from the /Users/andrea/Documents/advent_of_code_2019/aoc/ folder, so that it will be able to find my input file?
Thanks
Actually, I should have tried more before asking this question, because I just found the solution, but at this point I will write it here in case it can be useful to anyone:
If I change the path in this way:
with open('./aoc_03_input.txt', 'r') as file:
The file is being open correctly when I run the code in VSCode and when I run it from the terminal. Tested under OSX (but it should work under Linux too. Not sure about Windows).

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

"Cannot access setup.py: No such file or directory" - can't run any .py files?

This problem started while I was installing pyswip and needed to run a setup.py file. Using the command "python setup.py", I'm greeted with the following message: "python: can't open file 'setup.py': [Errno 2] No such file or directory."
I know this question's been asked a lot before, so I've tried everything in previous answers. Including #!/usr/bin/env python or #!/usr/bin/env python-3.3.0 at the very top of the script and then trying "chmod +x setup.py"
gives the following: "chmod: cannot access setup.py': No such file or directory".
Trying to run other .py files from the terminal gives the same result.
Running the file in the Python Shell from IDLE doesn't do anything.
Running the "ls -d */" command shows that the Python-3.3.0/ directory, where the .py files in question are, is definitely there.
Am I missing something really obvious? (If it helps, I have Elementary OS 0.2.)
When you run python setup.py that requires the setup.py file to be in the current directory.
You can control the current directory with the cd command.
So:
cd /home/acacia/Python-3.3.0/PySwip/pyswip-0.2.3
python setup.py install
I have no knowledge about Elementary OS but you could try to use the full path of the setup.py.
python /home/acacia/Python-3.3.0/PySwip/pyswip-0.2.3/setup.py install
[EDIT] Can't answer comments, so I just added the install parameter in my answer
You need to go into the directory that you are going to "setup". For example, if you are installing numpy, and you have git-cloned it, then it probably is located at ~/numpy. So first cd into ~/numpy, and the type the commend like "python setup.py build" there.

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