How to execute python script by terminal?
I wanted to learn Python, I installed some software. I created a little execution files and none of them will execute. So I've checked my terminal and type basic things like Is
, cd
etc. None of them work
:Users me$ cd
:~ me$ cd
:~ me$ cd /
:/ me$ Is
-bash: Is: command not found
:/ me$ cd ~
:~ me$ cd desktop
:desktop me$
If I type echo $PATH
, here the result:
/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I read many things about how to fix it but I don't understand. For example:
- I don't know how to find the User Folder
- I don't know how to get to the bash.profile
Bear in mind that I am very new to this.
Solution 1:
If you want to list all files/folders in a directory, you should type ls
not Is
. It is L, not i.
To learn python, you won't need to reach ~/.bash_profile
unless you need to change versions. Just use which python
or python --version
and use the syntax for the version that appears. Or use online compilers for quick testing.
Suppose you have your file learn.py
at Desktop, you type cd ~/Desktop
, type ls
to confirm, and then to run the file as a script, type python learn.py
. To enter python console, type python
and use it.
Wherever your file is, you could type cd
in Terminal, then drag your file into Terminal window and press Enter. Terminal will resolve the path for you.
For further help, refer to Stack Overflow. :)
If you want to see bash profile, open Finder, type command + shift + . to reveal hidden files and then command + shift + G and type ~/.bash_profile
in "Go To". But don't edit it unless required.
Solution 2:
if python is installed already just go to terminal, go to correct directory and type "python filename.py"