To those that have been helping me, I took Zed’s advice and bought the videos and started over. The videos make it more fun and I have already learned things I missed in the book. One thing that has puzzled me from the beginning is the path. I started with DOS computers and everything went in order. From the drive name, you then went to the program, then the file then to a subfile or whatever they were called. With these lessons it seems backwards when it comes to the ‘Python’ command. For example, first you have C:, then the users, then your name, then lpthw. Then the command of Python and the Exercise XXX.py. Now Python is not in the ‘lpthw’ directory, so why is it there in the path? I would think the proper way is for it to be like this, C:\Users\Yourname\Python\lpthw> exXXX.py. Do you smell what I’m cooking? Not2smart.
Glad you like the videos. They definitely help.
To attempt to answer your question in a way I hope might help, (so not strictly technically accurate):
So DOS…Christ even typing it sends shivers down my back after numerous cycles of the following…
Anyway, imagine Python is DOS. It’s a set of libraries that you run to enable you to interact with your OS and other stuff at the command line. Just like DOS. It’s global to the computer.
Your logic of root, user, folder, file is correct. But this is a file path. It’s where YOU choose to do all your work for the course. So it can be as simple as:
Now remember that Python, as an interactive command line interface, can be called from almost anywhere as it’s libraries are globally accessed. You couldn’t have the Python libraries in each users file path, as that would be loads of duplicated files for each user and each project location. (Remember this is not 100% accurate as you see when you meet ‘venv’ but you get the point). It would be the same as having DOS duplicated across everywhere you might use it.
So you set the PATH of Python so when you type ‘python’ to access its libraries, like accessing DOS, your system know where to go and look for the Python libraries (usually in the Systems.libraries area).
So there are three things here that I think might be confusing you:
- The folder called ‘Python’ in your file path. Maybe rename that folder to something else (like MyWork)
- Typing ‘python’ to access its interpreter (like accessing DOS command)
- The PATH to the Python libraries on your computer that sit in a globally-accessible location like Systems.Libraries.
OK, I will need to study up on the meaning of that.phrase, ‘globally accessed.’ Thank you. Not2smart
I found a very good and easy explanation how that
python command works. It’s writen for the Elm language but it is universaly true. Just change “elm” with “python”.
It’s for Linux but the logic is nearly the same on Windows and Mac. Hint: changed elm to python in the quote below.
Wait, what is the
When you run a command like
python, your computer starts by trying to find an executable file called
PATHis the list of directories that get searched. You can see these directories by running:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/binon my computer. The directories are separated by a
:so there are five possibilities listed here.
When I run
python some_script.py, my terminal starts by searching these five directories for an executable file named
python. It finds
/usr/local/bin/pythonand then runs
/usr/local/bin/python some_script.pywith whatever arguments I gave.
PATHenvironment variable is a convention that allows you to refer to a specific executable file without knowing exactly where it lives on your computer. This is actually how all “terminal commands” work! Commands like
lsare really executable files that live in directories listed in your
So the point of running
sudo mv python /usr/local/bin/(
sudo mv elm /usr/local/bin/) is to turn the
elm) binary into a terminal command, allowing us to call it just like
Note: Why do we need to use
sudofor that one command? Imagine if some program was able to add executables named
/usr/local/binthat did something tricky and unexpected. That would be a security problem! Many distributions make this scenario less likely by requiring special permissions to modify the
Sorry @Not2smart what I meant by ‘globally accessed’ was, you can open it from any directory on your computer. And not having to go to a specific folder/file location to open it.
I think you might need to jump to the Appendix A and go through the command line crash course. But, here’s the rundown on what you’re looking at:
This is your current working directory, as in where you are currently doing things. Think of this like a room in your hows named “Python\lpthw”. Your house–literally your home directory–is named “C:\Users\Yourname”. When you combine the two it’s like an address of “Zed @ 222 Users Street, Apartment Yourname”.
Now, if you use the
cd command you are Changing Directory, and that’s like walking over to a new room in your house:
cd ~ cd Python cd lpthw cd ..
In order those are:
- Go home, so ~ is an alias for “C:\Users\yourname”.
- Go to the Python room (change directory).
- Go to the lpthw room (change directory, cd).
- Oops, wrong room, leave lpthw and go back to the Python room.
You can even think of these being connected with a hallway that goes from Python to lpthw and back. But, there’s no hallway that goes from Users\yourname straight to lpthw, so this fails:
cd ~ cd lpthw FAIL!
That fails because lpthw is only connected (under, a child of) Python.
Now, your confusion is you think the command
python you type has to be in that very same room lpthw or else it won’t work. It’s like you walk into the lpthw room–let’s call it your tool shed–and you yell “I want to use my Python chainsaw” and magically the python chainsaw shows up and you’re like “WHIOAAAA!! WHAT IS THIS MAGIC!”
It’s not magic though, because there’s something called the PATH. It’s a little complicated at this point, but the PATH is a special variable that PowerShell knows about which is just a list of other directories to search through looking for commands you type. When you type:
You’re not saying “run the python chain saw that is in this room magically”. You’re saying:
- I want to run python and give it
ex1.pythe script to run.
- PowerShell first looks in lpthw, and does not find it.
- PowerShell looks up the PATH variable, and goes through each directory looking for a “python” command.
- It finally finds it, and then runs it, handing
ex1.pyas the first argument.
That’s it. How to see the path? Type this:
Or, this too:
$env:path -split ";"
That is the list of all directories that PowerShell searches looking for stuff to run. Do you see something that might have python in it? Let’s say you see:
It might not be that, but something like that. Well try this:
ls c:\Program Files\Python3.8\
You might then see a bin directory in there, or you might see python.exe or python3.exe. Poke around in there BUT DO NOT TOUCH ANYTHING IN THERE.
I think your issue is you are coming to this from DOS, and expect everything to work like DOS. This is like you come to my house, and you go, “Zed I’m going to the bathroom.” Then you sharply turn right and run into a wall, you get angry, you go “What the hell! Why isn’t there a bathroom here. There’s totally a bathroom here at my house DOS! This is weird!” No, you’re just in a new house and you’d take the time to ask where the bathroom is or look around and make some guesses.
So, DOS helps because you know what a bathroom is, and how a toilet works, but that’s about it. You’ve got to forget everything and relearn in it. Thus, the command line crash course at the end. Take a look at that but bug me if you get an index error. I believe I have to fix that. Use the index if you get a 404.
Try it. Assume nothing. Then see if you can move around and run commands.
Thanks again to all. I think I have a better understanding of it now. Hopefully the answers given will help others understand it too. Not2smart. (But getting smarter.)