▲ 14 ▼ How to import local files which import local files themselves? (kbin.earth) submitted 1 year ago by Limitless_screaming@kbin.earth to c/python@programming.dev 11 comments fedilink hide all child comments With a project structure like this: ├─main.py └─src ├─dep1.py └─dep2.py where the contents of each file is as follows: main.py: import src.dep1 as firstdep; print("Total success") dep1.py: import dep2 as seconddeb; print("success 1/3") dep2.py: print("success 2/3") Is the best way to do this creating an __init__.py file in src and importing src.dep2 in dep1.py? or is this a bad idea?
[–] rtxn@lemmy.world 3 points 1 year ago (5 children) Quick template: https://files.catbox.moe/g5tcy5.tar Also, don't delete this post. Others might need this information too. permalink fedilink source hideshow 5 child comments replies: [–] Limitless_screaming@kbin.earth [S] 2 points 1 year ago (4 children) ofc. It's weird how no one asked this before with how little help articles mention __init__.py. permalink fedilink source parent hideshow 4 child comments replies: [–] rtxn@lemmy.world 4 points 1 year ago (3 children) https://docs.python.org/3/tutorial/modules.html#packages The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). This prevents directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. permalink fedilink source parent hideshow 3 child comments replies: [–] Limitless_screaming@kbin.earth [S] 0 points 1 year ago (2 children) Are you accusing me of not reading tfm? because I did, but was expecting this specific situation to be clarified on stackOverFlow, geeksForGeeks or somewhere similar. Since it seemed like this import pattern should be common. permalink fedilink source parent hideshow 2 child comments replies: [–] rtxn@lemmy.world 3 points 1 year ago (1 child) I was pointing out a part of the documentation that you may have overlooked. In good faith, I might add, because the documentation is comprehensive but horribly signposted. Everything I wrote in my first comment is there, but in a less human, more technical phrasing. permalink fedilink source parent hideshow 1 child comment replies: [–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent
[–] Limitless_screaming@kbin.earth [S] 2 points 1 year ago (4 children) ofc. It's weird how no one asked this before with how little help articles mention __init__.py. permalink fedilink source parent hideshow 4 child comments replies: [–] rtxn@lemmy.world 4 points 1 year ago (3 children) https://docs.python.org/3/tutorial/modules.html#packages The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). This prevents directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. permalink fedilink source parent hideshow 3 child comments replies: [–] Limitless_screaming@kbin.earth [S] 0 points 1 year ago (2 children) Are you accusing me of not reading tfm? because I did, but was expecting this specific situation to be clarified on stackOverFlow, geeksForGeeks or somewhere similar. Since it seemed like this import pattern should be common. permalink fedilink source parent hideshow 2 child comments replies: [–] rtxn@lemmy.world 3 points 1 year ago (1 child) I was pointing out a part of the documentation that you may have overlooked. In good faith, I might add, because the documentation is comprehensive but horribly signposted. Everything I wrote in my first comment is there, but in a less human, more technical phrasing. permalink fedilink source parent hideshow 1 child comment replies: [–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent
[–] rtxn@lemmy.world 4 points 1 year ago (3 children) https://docs.python.org/3/tutorial/modules.html#packages The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). This prevents directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. permalink fedilink source parent hideshow 3 child comments replies: [–] Limitless_screaming@kbin.earth [S] 0 points 1 year ago (2 children) Are you accusing me of not reading tfm? because I did, but was expecting this specific situation to be clarified on stackOverFlow, geeksForGeeks or somewhere similar. Since it seemed like this import pattern should be common. permalink fedilink source parent hideshow 2 child comments replies: [–] rtxn@lemmy.world 3 points 1 year ago (1 child) I was pointing out a part of the documentation that you may have overlooked. In good faith, I might add, because the documentation is comprehensive but horribly signposted. Everything I wrote in my first comment is there, but in a less human, more technical phrasing. permalink fedilink source parent hideshow 1 child comment replies: [–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent
[–] Limitless_screaming@kbin.earth [S] 0 points 1 year ago (2 children) Are you accusing me of not reading tfm? because I did, but was expecting this specific situation to be clarified on stackOverFlow, geeksForGeeks or somewhere similar. Since it seemed like this import pattern should be common. permalink fedilink source parent hideshow 2 child comments replies: [–] rtxn@lemmy.world 3 points 1 year ago (1 child) I was pointing out a part of the documentation that you may have overlooked. In good faith, I might add, because the documentation is comprehensive but horribly signposted. Everything I wrote in my first comment is there, but in a less human, more technical phrasing. permalink fedilink source parent hideshow 1 child comment replies: [–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent
[–] rtxn@lemmy.world 3 points 1 year ago (1 child) I was pointing out a part of the documentation that you may have overlooked. In good faith, I might add, because the documentation is comprehensive but horribly signposted. Everything I wrote in my first comment is there, but in a less human, more technical phrasing. permalink fedilink source parent hideshow 1 child comment replies: [–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent
[–] Limitless_screaming@kbin.earth [S] 3 points 1 year ago Sorry, that was just a joke that came off wrong. I understand you were just trying to help. permalink fedilink source parent