|
| 1 | +# Using the command line |
| 2 | + |
| 3 | +It is often necessary (or quicker) to run Python from the command line. This is a *very* quick guide to using a command line, it will get you started. |
| 4 | + |
| 5 | +The first step is to open your terminal. How you do this depends on the type of PC you are using. |
| 6 | + |
| 7 | +On Windows see if the following are installed: |
| 8 | +- Terminal (this is a more modern implementation) |
| 9 | +- Powershell (if you have conda installed run Anaconda Powershell Prompt) |
| 10 | +- Cmd (this is considered a bit out of date now, if you have conda installed run Anaconda Prompt) |
| 11 | + |
| 12 | +On a Mac |
| 13 | +- Terminal |
| 14 | + |
| 15 | +On Linux it will depend on your distro. Generally look for an app called one of the following: |
| 16 | +- Terminal |
| 17 | +- Command |
| 18 | +- Prompt |
| 19 | +- Shell |
| 20 | + |
| 21 | +Once opened we can do various things. Initially it should look something like this: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Depending on the 'shell' that you have you might have more information, such as your user name. We can now enter commands into this prompt. |
| 26 | + |
| 27 | +Most useful will be |
| 28 | + |
| 29 | +| Command | Effect | |
| 30 | +|---|---| |
| 31 | +| *cd* | Followed by a path will change directory | |
| 32 | +| *ls* | Lists the contents of the current directory | |
| 33 | +| *mkdir* | Followed by a name will create a directory of that name | |
| 34 | + |
| 35 | +When we type a path we can do so in two ways. Either absolute or relative. Absolute is a full path from the users *root* directory. This is normally the folder you start in when you open a terminal. In the example above we start in Users/User. Say we want to open the Documents folder that is in this User folder. We can type the following: |
| 36 | + |
| 37 | +```bash |
| 38 | + cd Users/User/Documents |
| 39 | +``` |
| 40 | +However, we can also do a relative path. |
| 41 | + |
| 42 | +```bash |
| 43 | +cd ./Documents |
| 44 | +``` |
| 45 | +Here the path is relative to which ever directory we are *currently* in. A single period means the current folder while two periods means go up a level. |
| 46 | + |
| 47 | +For example, when we are in the Users/User/Documents folder, if we type: |
| 48 | + |
| 49 | +```bash |
| 50 | +cd ../ |
| 51 | +``` |
| 52 | +Then we will move back into the Users/User folder. Or again starting in Users/User/Documents |
| 53 | +```bash |
| 54 | +cd ../../ |
| 55 | +``` |
| 56 | +this will move us up two levels in the Users folder |
| 57 | + |
| 58 | +To then run a Python script, easiest is to navigate to the folder where the script is and run the following (assuming that the script is call python_prog.py) |
| 59 | + |
| 60 | +```bash |
| 61 | +python ./python_prog.py |
| 62 | +``` |
| 63 | + |
0 commit comments