0

i've been self coding in python for a while now but i never ever could learn how to structure my projects into proper structure. this is what i am doing right now

my_project/
    src/
        __init__.py
        dog.py
        animal.py
    main.py
    

each script looks like this

# animal.py 
class animal:
    def __init__(self, kind):
        self.kind = kind

if __name__=='__main__':
    a=animal("mammal") 

#dog.py 
if __name__=='__main__':
    from animal import animal
else:
    from .animal import animal

        
class dog(animal):
    def __init__(self, name, age):
        super().__init__('Mammal')
        self.name = name
        self.age = age
        
        
if __name__=='__main__':
    d=dog("Fido", 3)


#__init__.pyt 
from .animal import * 
from .dog import * 


# main.py 
import src 
d=src.dog("Fido", 3)

My question is is this an at least ok way of structuring things, the imports in dog.py are extremelly ugly imho and i couldn't find any other way to be able to both run the dog.py itself as part of development below if name part and also being able to make this script import things correctly when i run stuff from outside the src/ directory (main.py)

pawelek69420
  • 134
  • 1
  • 5
  • 1
    Does this answer your question? [What is the best project structure for a Python application?](https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application) – blunova Jul 22 '23 at 13:28
  • 1
    Why don't you just remove the if name equals main part and instead make separate file that imports from dog.py or animal.py and runs the code? – InfoDaneMent Jul 22 '23 at 15:27
  • 1
    For example, just make a dog_test.py file in src or base directory and then import dog.py. When you want to only import during production, you can import directly from the dog.py file in your main file. When you want to test your code in development, use dog_test.py – InfoDaneMent Jul 22 '23 at 15:29

0 Answers0