Reputation: 833
I'm new with python, I've been learning for a few weeks. However now I've just changed my OS and I'm now using ubuntu and I can't run any script on my terminal.
I made sure to have the #!/usr/bin/env python
but when I go to the terminal and type, for example python test.py
the terminal shows an error message like this
python: can't open file 'test.py': [Errno 2] No such file or directory
what do I do?
I must save the file in any specific folder to make it run on terminal?
Upvotes: 15
Views: 214910
Reputation: 1
First create the file you want, with any editor like vi or gedit. And save with a .py
extension. In that the first line should be
#!/usr/bin/env python
Upvotes: -9
Reputation: 1
Sorry, Im a newbie myself and I had this issue:
./hello.py: line 1: syntax error near unexpected token "Hello World"'
./hello.py: line 1:
print("Hello World")'
I added the file header for the python 'deal' as #!/usr/bin/python
Then simple executed the program with './hello.py'
Upvotes: -1
Reputation: 11
Set the PATH as below:
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python"
and press Enter.
In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python"
and press Enter.
In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python"
and press Enter.
Note − /usr/local/bin/python
is the path of the Python directory
now run as below:
-bash-4.2$ python test.py
Hello, Python!
Upvotes: 1
Reputation: 13
Save your python file in a spot where you will be able to find it again. Then navigate to that spot using the command line (cd /home/[profile]/spot/you/saved/file) or go to that location with the file browser. If you use the latter, right click and select "Open In Terminal." When the terminal opens, type "sudo chmod +x Yourfilename." After entering your password, type "python ./Yourfilename" which will open your python file in the command line. Hope this helps!
Running Linux Mint
Upvotes: 0
Reputation: 44334
This error:
python: can't open file 'test.py': [Errno 2] No such file or directory
Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)
I must save the file in any specific folder to make it run on terminal?
No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.
Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".
Thus, if you type something like:
python test.py
test.py
needs to be in the current working directory. In Linux, you can change the current working directory with cd
. You might want a tutorial if you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bash
for a shell, so it should apply to you.)
Upvotes: 25