Modern Python Projects Transcripts
Chapter: Managing Python project
Lecture: How Python imports modules?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's first talk about how to structure your Python project.
0:03 Unfortunately, there is no perfect way to do this.
0:06 What might work for one project won't work for a different one.
0:10 And since Python doesn't enforce any specific structure,
0:13 you can organize your project however you want.
0:16 You might be wondering, Why does a good project structure matter?
0:19 First of all, a good project structure makes it easier to understand the project.
0:23 If you put files in random folders or throw hundreds of files inside,
0:27 one folder. It's going to be difficult to understand, how this project works and what's the
0:33 connection between all those files? And it's going to be especially difficult for new developers
0:38 on your team. So as your project grows,
0:40 you sometimes need to refactor the file structure.
0:43 For example, if you have a lot of scripts related to managing users of your
0:47 application, maybe it's time to put all of them inside the folder called Users.
0:53 Understanding How the project works is very important,
0:56 but a bad project structure can actually cause some import errors.
1:00 They usually result in a module
1:02 not found error, or import error.
1:05 To understand why we have import errors. Let's quickly see how imports work in Python, when
1:11 you import a module Python will look in three places.
1:14 The first place is the folder with the Python file that you are running.
1:18 Let's say we have a python file called start_server that it's located inside my modules
1:23 /my_module/scripts folder. This file imports,
1:27 some methods from a module called utils.
1:30 When you run this file with Python Command,
1:32 the utils module have to be inside the scripts directory.
1:36 Otherwise, python won't be able to find it.
1:39 It's slightly different when you don't run a script,
1:42 but you start on interactive python session by running Python command in your terminal.
1:47 If you try to import some python modules in this interactive session,
1:51 python will look inside the current folder.
1:54 If Python can't find the module that you want to import,
1:57 it will then check the additional paths from the Python path environment variable. By default.
2:02 This variable is empty, but you can use it to specify some additional paths for
2:07 importing python modules. And finally,
2:10 there are some installation dependent folders that python will check.
2:14 Installation dependent means that they depend on what operating system you use,
2:18 but also on how you installed python.
2:21 Since we're using pyenv.
2:23 It will contain some folders from the pyenv directory.
2:27 That's a lot of places to check,
2:28 but there is an easy way to list all the places where Python will look for
2:32 modules. All you need to do is run.
2:35 import sys and then sys.path.
2:38 This will print the list of folders.
2:40 That python would check, if you try to import some modules.
2:44 As you can see here, we are not using a virtual environment,
2:47 so Python will look for pip packages inside the global site packages.
2:52 But when we are inside the virtual environment,
2:54 the last item on this list has changed to the virtual environment.
2:59 Let me show you a better comparison.
3:01 As you can see. First we have the current directory.
3:04 Then we have three folders related to my operating system,
3:08 and then I have another folder,
3:10 depending on whether I'm using the virtual environment or not.
3:13 This one, it's pointing to site packages.
3:16 Checking the syst.path is often the first step of the debugging,
3:19 import errors and speaking of import errors. Lets talk about two most common ones in the next lesson.