Puzzled by the path

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…

C:\FORMAT C

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:

C:/user/the_folder_for_lpthw/a_load_of_examples_with_the_extension_.py

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:

  1. The folder called ‘Python’ in your file path. Maybe rename that folder to something else (like MyWork)
  2. Typing ‘python’ to access its interpreter (like accessing DOS command)
  3. The PATH to the Python libraries on your computer that sit in a globally-accessible location like Systems.Libraries.

HTH

1 Like

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”.

Link to the original

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 PATH variable?

When you run a command like python, your computer starts by trying to find an executable file called python .

The PATH is the list of directories that get searched. You can see these directories by running:

echo $PATH

This prints /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin on 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/python and then runs /usr/local/bin/python some_script.py with whatever arguments I gave.

So the PATH environment 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 ls are really executable files that live in directories listed in your PATH variable.

So the point of running sudo mv python /usr/local/bin/ (sudo mv elm /usr/local/bin/) is to turn the elm (elm) binary into a terminal command, allowing us to call it just like ls and cd .

Note: Why do we need to use sudo for that one command? Imagine if some program was able to add executables named ls or cd to /usr/local/bin that 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 /usr/local/bin/ directory.

Link to the original above

2 Likes

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:

C:\Users\Yourname\Python\lpthw> 

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:

  1. Go home, so ~ is an alias for “C:\Users\yourname”.
  2. Go to the Python room (change directory).
  3. Go to the lpthw room (change directory, cd).
  4. 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:

python ex1.py

You’re not saying “run the python chain saw that is in this room magically”. You’re saying:

  1. I want to run python and give it ex1.py the script to run.
  2. PowerShell first looks in lpthw, and does not find it.
  3. PowerShell looks up the PATH variable, and goes through each directory looking for a “python” command.
  4. It finally finds it, and then runs it, handing ex1.py as the first argument.

That’s it. How to see the path? Type this:

$env:path

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:

C:\Program Files\Python3.8

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.

Course Correction

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.

1 Like

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.)

2 Likes