-2

I have __init__.py file in current directory.

I need a complete list of circumstances, under which this file will run.

First case is

import __init__

written in the script.py in the same directory and this file runs.

What are other cases?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    `import __init__` is really weird. Maybe you want `import . as …`? Wanting to “run” a module rather than make use of its contents is indicative of bad design, though (modules with side effects on import suck). Consider writing a function, importing (`from . import main`), and calling it. – Ry- Sep 14 '17 at 09:50
  • I've flagged this as unclear. You've said what you don't want to write, but what _do_ you want to write? What do you want that to achieve? Could you write a minimal working example? – ymbirtt Sep 14 '17 at 09:51
  • This is my question: I don't want to import __init__, I want it to run automatically – Dims Sep 14 '17 at 09:51
  • 1
    No, you actually don’t want that. – Ry- Sep 14 '17 at 09:51
  • @ymbirtt example of what? I wan't `__init__.py` initialize package. – Dims Sep 14 '17 at 09:52
  • @Ryan I swear I do – Dims Sep 14 '17 at 09:52
  • 1
    https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Ry- Sep 14 '17 at 09:53
  • An example of some code that exhibits the problem you're trying to demonstrate. What code would you like to put in `__init__.py`? What do you want to be able to write in order to cause that code to run? Why do you want to be able to do that? – ymbirtt Sep 14 '17 at 09:54
  • @ymbirtt I want to put package initialization into `__init__.py`, particularly, setting `sys.path` – Dims Sep 14 '17 at 09:55
  • Why do you want to set `sys.path` when you initialize the package? – ymbirtt Sep 14 '17 at 09:57
  • @ymbirtt it's another question, you can answer it, if you know the answer: https://stackoverflow.com/q/46215769/258483 – Dims Sep 14 '17 at 09:59
  • The best answer to your problems seems to use a virtual environment and to define a setup.py to define the entry point (be it in your `__init__.py` or wherever else). Then you can (dev) install your own package in the venv and you can import any library from you own package. I would advise to check this: https://jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/ (even if you do not have to open source it, the advices about directory structure, use of setuptools etc. still apply) – zezollo Sep 14 '17 at 10:04
  • @zezollo it is very complex; I don't beleive there is no other solution – Dims Sep 14 '17 at 10:05
  • 1
    Everything you think you want to do is wrong, so cut it out. Make a package. Use absolute imports based on the package name for scripts, or skip that completely and run things through `python -m`. Do not mess with `sys.path`. – Ry- Sep 14 '17 at 10:05
  • What are "absolute imports"? – Dims Sep 14 '17 at 10:06
  • https://www.google.com/search?q=python+"absolute+import" – Ry- Sep 14 '17 at 10:07
  • @Dims, yeah, it requires to setup some boilerplate, but then you definitely get rid of import problems in python (except circular ones). You don't need to modify sys.path or any environment variable... it's easier. If you want to test the install process of your package, just create a new venv and you can install it inside. Very practical, really... – zezollo Sep 14 '17 at 10:08
  • @Ryan okay my imports are already absolute and this doesn't solve the question – Dims Sep 14 '17 at 10:09
  • @zezollo I don't beleive python has such a bad design; there should be better solution – Dims Sep 14 '17 at 10:10
  • @Dims: Did you just ignore every part of my comment except the words “absolute imports”? Anyway, I’ve left a complete description of what you need to do to solve the problem; if you do some research of your own and put together a proper (non-XY-problem) question, maybe you’ll figure it out. (Given you’ve already asked 1,039 of them I’m not hopeful though?) – Ry- Sep 14 '17 at 10:18
  • @Ryan "absolute imports" was a term I don't understand, and `python -m` hint is what I upvoted for, now checking... – Dims Sep 14 '17 at 10:21
  • @Ryan no, `python -m` also does not run `__init__.py` in current directory – Dims Sep 14 '17 at 10:28

1 Answers1

0

A __init__.py file is run when the package that the corresponds to it is imported. So a file some_package\__init__.py is executed when you import some_package. When you import a submodule from a package the package is first loaded. So import aa.bb.cc, will load aa (and thus execute aa/__init__.py) before loading aa.bb and aa.bb.cc.

The folder some_package must be discoverable, which means that it must exists in one of the sys.path folders. This includes the current directory.

If you simply run a script (python some_script.py) and there happens to be a __init__.py file in the same folder then this means nothing, since the current folder is not a package itself. (unless of course if you execute a script that happens to reside inside a package).

driax
  • 2,528
  • 1
  • 23
  • 20
  • one extra: if you want to be able to run the package you can construct a __main__.py file (see [here](https://stackoverflow.com/questions/4042905/what-is-main-py)) – Nemelis Sep 14 '17 at 13:11