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