15  Projects Structure

15.1 Task : Manage Python Projects Structure

15.1.1 Project 1

  • Create a folder named project1 which will use the environment/kernel named env1
mkdir project1
  • Use the correct naming for your project, packages and modules.

  • Create a package named istec.p4.exercises

mkdir istec
cd istec
mkdir p4
cd p4
mkdir exercises

```{python} #| eval: false cd exercises

Note

You may create the above folders using the code editor or the operating system file explorer, but for that, you should not need a guide.

  • To tell Python that you are creating a package, you have to put a file named __init__.py on every single folder of that package.

  • Within this package, in folder exercises create a module (which is a Python file) named exercise1.py

  • Your folder tree should now be similar to this when looked from inside folder project1

.
└── istec
    ├── __init__.py
    └── p4
        ├── __init__.py
        └── exercises
            ├── __init__.py
            └── exercise1.py
  • Inside this module, create a basic function named hello to execute print(“Hello World!!”)
def hello():
    print("Hello World!!")

# This is for demo purposes only.
# There is no need to call the function here.
# Unless you want to see the output of it right away.
hello() 
  • Execute your program

15.1.2 Project 2

  • Start from project1 code and duplicate it as project2

  • Rename file exercise1.py to exercise2.py

  • Your folder tree should now be similar to this when looked from inside folder project2

.
└── istec
    ├── __init__.py
    └── p4
        ├── __init__.py
        └── exercises
            ├── __init__.py
            └── exercise2.py
  • Update your code in exercise2.py to produce an output similar to this:
    • Hello World!!
    • I’m running using virtual environment: env1. (or)
    • I’m running using virtual environment: env2. (for when you are running it using another virtual environment)
  • In file exercise2.py update the function hello
import sys

def hello():
    print("Hello World!!")
    print("I'm running using virtual environment:", sys.prefix )

# This is for demo purposes only.
# There is no need to call the function here.
# Unless you want to see the output of it right away.
hello() 
  • Execute your program using environment/kernel env1 and then with environment/kernel env2

  • Output of using env1.

Hello World!!
I'm running using virtual environment: /Users/joaocaldeira/istec/env1
  • Output of using env2.
Hello World!!
I'm running using virtual environment: /Users/joaocaldeira/istec/env2
  • Confirm that the outputs should be different based on the selected environment.

  • At the end of these exercises, your istec folder should have the following structure.

├── env1
├── env2
├── env3
├── project1
│   └── istec
│       ├── __init__.py
│       └── p4
│           ├── __init__.py
│           └── exercises
│             ├── __init__.py
│             └── exercise1.py
└── project2
    └── istec
        ├── __init__.py
        └── p4
            ├── __init__.py
            └── exercises
                ├── __init__.py
                └── exercise2.py