Create Your First Python Project

Structure a Python project with multiple files, a requirements.txt, and a clean folder layout.

6 min read

Single .py files are fine for experiments. But when you start building something you want to keep, reuse, or share, a structured project folder makes everything easier.

Before starting, make sure you can write and run a Python script and install packages with pip.

Create the project folder

Pick a location for your projects. A folder called projects or dev in your home directory works well:

bashbash
mkdir ~/projects
cd ~/projects

Create a new folder for this project:

bashbash
mkdir number-guesser
cd number-guesser

Use a descriptive name with hyphens between words. This is the Python convention. Avoid spaces, which can cause problems in terminal commands.

Create the main script

Inside number-guesser, create a file called main.py:

pythonpython
import random
 
def get_random_number():
    return random.randint(1, 100)
 
def get_user_guess():
    while True:
        try:
            return int(input("Guess a number between 1 and 100: "))
        except ValueError:
            print("Please enter a valid number.")
 
def main():
    target = get_random_number()
    guess = None
    attempts = 0
 
    print("I am thinking of a number between 1 and 100.")
 
    while guess != target:
        guess = get_user_guess()
        attempts += 1
 
        if guess < target:
            print("Too low.")
        elif guess > target:
            print("Too high.")
 
    print(f"You got it in {attempts} attempts!")
 
if __name__ == "__main__":
    main()

Run it to make sure it works:

bashbash
python main.py

The if __name__ == "__main__" block is a Python convention. Code inside it only runs when you execute the file directly. If another file imports this one, the main() function does not run automatically. This lets you reuse functions without triggering the full program.

Split code into modules

When main.py grows, move related functions into separate files. Create a new file called game_logic.py:

pythonpython
import random
 
def get_random_number():
    return random.randint(1, 100)
 
def check_guess(guess, target):
    if guess < target:
        return "Too low."
    elif guess > target:
        return "Too high."
    return None

Now update main.py to import from game_logic.py:

pythonpython
from game_logic import get_random_number, check_guess
 
def get_user_guess():
    while True:
        try:
            return int(input("Guess a number between 1 and 100: "))
        except ValueError:
            print("Please enter a valid number.")
 
def main():
    target = get_random_number()
    guess = None
    attempts = 0
 
    print("I am thinking of a number between 1 and 100.")
 
    while guess != target:
        guess = get_user_guess()
        attempts += 1
        result = check_guess(guess, target)
        if result:
            print(result)
 
    print(f"You got it in {attempts} attempts!")
 
if __name__ == "__main__":
    main()

main.py is now shorter and easier to read. game_logic.py contains functions that are easy to test and reuse in other projects. As your project grows, keep splitting related code into modules with clear names.

Create a requirements.txt

Even if your project only uses the standard library (as this one does with random), adding a requirements.txt is good practice. Create it now so the habit sticks:

bashbash
python -m pip freeze > requirements.txt

For this project, the file will be empty or contain only pip and setuptools. For a project that uses external packages like requests, the file would list them with versions:

plaintextplaintext
requests==2.32.4

If someone clones your project, they can install everything with:

bashbash
python -m pip install -r requirements.txt

For more details on pip workflows, see install and manage Python packages.

Add a README

Create a README.md file at the root of your project:

markdownmarkdown
# Number Guesser
 
A simple console game where the player guesses a random number between 1 and 100.
 
## How to run
 
python main.py
 
## Requirements
 
Python 3.10 or later.

A README tells anyone who finds your project (including your future self) what it does and how to run it. The .md extension means Markdown, a simple formatting language that GitHub and many editors render nicely.

The project folder structure

Your project now looks like this:

plaintextplaintext
number-guesser/
  main.py
  game_logic.py
  requirements.txt
  README.md

This is a clean, standard layout for a beginner Python project. Every file has a clear purpose. The folder name matches the project name. The structure is flat enough to navigate easily.

For larger projects, you might add subfolders:

plaintextplaintext
number-guesser/
  number_guesser/
    __init__.py
    main.py
    game_logic.py
  tests/
    test_game_logic.py
  requirements.txt
  README.md

The __init__.py file makes number_guesser a Python package. It can be empty. The tests/ folder holds automated tests, which you will learn about later when you reach the testing topics. For now, the flat structure is enough.

Use a virtual environment

A clean project pairs naturally with a virtual environment. Virtual environments keep each project's packages isolated from every other project.

Create one inside your project:

bashbash
python -m venv .venv

Activate it:

  • macOS/Linux: source .venv/bin/activate
  • Windows: .venv\Scripts\activate

Now when you install packages with pip, they go into .venv instead of your global Python installation. The .venv folder should not be committed to version control. Add it to your .gitignore file:

plaintextplaintext
.venv/
__pycache__/

Next steps

Your project is structured and ready to grow. Now set up a virtual environment if you have not already, and learn how to organize your Python workspace for a smooth daily workflow.

When you are ready to learn the language itself, start with Python syntax and indentation in the Python Basics section.

Rune AI

Rune AI

Key Insights

Keep your project in a dedicated folder with a clear name; Use requirements.txt to list dependencies; Split related code into separate .py files; Add a README.md to explain what the project does and how to run it.

RunePowered by Rune AI

Frequently Asked Questions

What folders and files does a simple Python project need?

A minimal project needs at least one .py file. A practical beginner project typically has a main script, a requirements.txt for dependencies, a README.md for documentation, and possibly a helper module or two.

Should I put all my code in one file?

For very small scripts, one file is fine. As soon as you have reusable functions or logic that serves a distinct purpose, split it into separate .py files. This makes the project easier to read and maintain.

What is __init__.py?

__init__.py tells Python that a folder should be treated as a package. It can be empty. Without it, you cannot import modules from that folder using dot notation like from mypkg import helper.

Where should I create my Python project folder?

Anywhere convenient. A common choice is a projects or dev folder inside your home directory. Avoid deeply nested system folders and folders that sync to cloud storage, which can sometimes interfere with virtual environments.

Conclusion

A clean project structure helps you and others understand your code quickly. Start with a single main script, add a requirements.txt, and split logic into modules as your project grows.