Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Recognizing How the Structure Still Isn't Ideal

You’ve worked hard to improve your project’s structure! However, there’s still room for improvement. Now that you’ve gotten rid of -m, you can’t find the package anymore.

You’ll tackle the following issues:

  • How -m and path have different PYTHONPATH values
  • How you could manually add to PYTHONPATH, but it’s a bad idea
  • How adding to PYTHONPATH manually comes with a scaling problem

00:00 So now it seems like we’ve solved the problem, so now try to run the one without -m.

00:10 And so, we’ve kind of moved our problem a little bit. Right. And really the explanation for this is the same as earlier, that it’s looking in a different place, and now, as we saw earlier, snake was in path when we didn’t use -m, but that means that snakesay is not in path, essentially.

00:29 So now we’ve kind of flip-flopped our problem. It works in one way if we do the absolute import like this, but it won’t work in the way we were doing before.

00:38 And that’s because of the difference, because of the snakesay without the -m or the snakesay with the -m. Right, those give different paths, essentially, so different place they search for. Okay, so is there a way we can make it work for both? Yes.

00:52 And what you might be tempted to do is now, again, “Well, let’s just fix the path. Let’s append to path and put everything on path.”

01:00 Right, and you’d do that with sys.path.append(), and then you’d manually sort of type out the path that you would want to put in here, which would—then you have to double-escape (\\) it because it’s Windows—add RealPython, and mess around with things like that. Right. Which is really common.

01:18 I mean, you’ll probably see that in a lot of places, and a lot of people might even recommend this to you as being sort of the simpler solution. Yeah. It’ll solve problems seemingly right now, but again, it doesn’t really scale well.

01:32 What’s a typical scaling problem that you might run into with this? So, one is that if you kind of have several projects that you kind of start hooking together,

01:41 then you might need to add more stuff to path. And another thing is that if you would share this with me and my file layout looks differently, then I would need to go in and change all the paths to match mine. Right.

01:54 Yeah, because of you might have a different drive or you might be on a different system which doesn’t have drive letters. Right. And that just gets really messy really quickly. Exactly. Okay, so we want to stay away from sys.path as much as possible, I think. Yeah, we can just stay completely away from it.

Become a Member to join the conversation.