Identifying Issues With the Current Structure
In this lesson, you’ll learn that imports may fail depending on where you call your script. You’ll also get to know PYTHONPATH
and understand why modifying it isn’t a good idea.
00:00
Okay, so currently we have our script running in this way where we can just say snakesay
. We don’t have to call the actual CLI file, which is now named __main__
.
00:09
And I’ve seen it’s common to use a -m
, or “minus m,” flag here, which I understand to mean module to basically do the same thing. We’re saying python -m snakesay
, which is the module, right? Yeah.
00:26
But if we try and run this, we run into a ModuleNotFoundError
saying it can’t find 'snake'
, and it’s saying it can’t find 'snake'
from __main__.py
. So let’s go to that line.
00:37
So, it’s failing on line 3 here, saying it can’t find snake
. Why is this? Yeah. So, if you go back a little bit to the terminal first, so the difference between what we used to run, python snakesay
, and now python -m snakesay
, is that this python snakesay
directory actually looks in the file system, and it runs the directory.
00:59
Which we talked about. Running a directory means running the __main__
file. With the -m
, what we’re asking Python to do is to kind of look in its path for a modular directory called snakesay
. When you say “look in its path,” which path do you mean? Right, there’s kind of two different paths that are fighting here.
01:20
So in the command-line interface, so in the terminal, we were kind of, I guess now you can see we’re in C:\RealPython
, and that’s kind of our active path in the terminal. Right, our current working directory. Right.
01:32
And then Python maintains another path, which just contains all the libraries that Python has installed: both the standard library, so if you do things like import math
, it knows where to find this, but also if you’ve kind of installed other packages like pandas
or django
, things like this, it’s able to find them. Right.
01:49
So when we install something like Django and we say import
django
, that’s basically Python knows where to look for that. Exactly.
01:58
So it kind of has a list of different paths that it can search for django
. So in a similar way to the command-line environment having a path variable where it searches for all executables, Python has its own path version where it’ll search for a module when you try and import it. Exactly.
02:17 And as all things in Python, this is something that we can play and change. However, that’s a really bad idea. Really bad idea? Okay. But it’s so common.
02:29 Yes. It’s one of those things that, “But it works, so let’s just do it!” It does work. Yeah. The problem with it is that it doesn’t really scale. Right.
02:37 So if kind of you start doing it with one script and then it kind of, you
02:42 sometimes then end up being, “Okay, now I need to change the path again for I’m trying to run this with another script,” and things get messy.
Become a Member to join the conversation.