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

Unlock This Lesson

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

Unlock This Lesson

Scaffold the Project Structure

If you’d like some help with managing multiple Python versions on your machine, then check out Start Managing Multiple Python Versions With pyenv.

00:00 Scaffold the Project Structure. The first step is to use your favorite code editor or a cloud-based IDE to create a new Python project while specifying an isolated virtual environment for the dependencies needed.

00:14 The minimum interpreter version required for this project is Python 3.10 due to a few syntactic constructs that you’ll be using. If you can, consider switching to a more recent release for better performance and other improvements.

00:29 If you want to know about managing multiple Python versions on your computer, then take a look at this Real Python course. Once you have the project set up in your editor, scaffold the initial folder structure with these two nested Python packages, both of which should be empty at this point. By placing the project’s root package under the src/ subfolder, you follow the so-called src layout convention for organizing files in a project.

00:55 Note that the pyproject.toml files should live outside of the src/ subfolder. The alternative is a flat layout with all the files in the same folder.

01:07 You can read about their differences in the official Python Packaging User Guide. In a nutshell, the src layout is preferred for larger projects because it allows you to better separate the project code from other files, such as tests.

01:24 While the project’s name is maze-solver with a hyphen, you name the corresponding Python package using an underscore (_) to form a valid Python identifier, which follows the same rules as variable names.

01:38 The rules for naming a Python project, or a distribution package in slightly more technical terms, are more liberal. If you’re curious enough, you can check out PEP 508 to find the regular expression that validates these names.

01:53 To install your project in an active virtual environment, you’ll need to specify the minimum required configuration in the TOML file. This will include the name and the version of the project.

02:05 Go ahead and open the pyproject.toml file and enter the content seen on-screen. This

02:14 is a pretty standard configuration and a good starting point for most Python projects and libraries. Note that you don’t have to explicitly state the folder layout you are using because build tools such as setuptools will automatically find the Python source code. If you’re planning to publish your package on PyPI, then you should pick a globally unique name that won’t conflict with an existing Python distribution package. Other than this, you have a fair amount of freedom in choosing your project name.

02:46 It’s good practice to work in a Python virtual environment when you’re working with third-party packages or installing your own. So, on-screen you’ll see one being created on macOS with the commands that will also work on Linux.

03:08 And here are the commands needed when working on Windows.

03:24 After you’ve saved your changes in the pyproject.toml file, you can install maze-solver with pip by running the command seen on-screen from the root directory of the project.

03:38 During the development of a src-layout project, it’s advisable to use the --editable flag or its -e alias to ensure that any changes you make to your code are immediately reflected in the virtual environment. Otherwise, you’d need to manually reinstall the package each time you edit the code.

03:56 If you run into an error about not being able to install the directory in editable mode due to a missing or setup.cfg file, then you may need to upgrade pip itself.

04:17 To confirm that you’ve successfully installed the maze_solver package in your virtual environment, open up your REPL and try importing the package.

04:28 If everything works fine, then you shouldn’t see any output or error messages after running this line of code. Otherwise, you’ll immediately get a ModuleNotFoundError.

04:40 With the scaffolded Python project in place, you can now proceed to coding an object-oriented representation of the maze, and that’s what you’ll be doing next.

Become a Member to join the conversation.