0

A variable isDevelopment is inside the manager/__init__.py file:

 isDevelopment = True

Within the same directory a file fusion.py attempts to import it at the file level:

from . import isDevelopment

Note: pycharm is ambivalent to it: the import is not flagged in any case:

enter image description here

When attempting to import it from some other location e.g. .. pycharm does complain:

enter image description here

When running

 python3 manager/fusion.py

the following occurs:

 ImportError: cannot import name 'isDevelopment' from '__main__'

Another attempt per one of the suggestions:

from ..manager import isDevelopment

This results in:

ValueError: attempted relative import beyond top-level package

Why is this attempted import not working - and what needs to be changed?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

2 Answers2

1
./test.py
./manager/__init__.py
./manager/fusion.py

__init__.py

isDevelopment = True

./manager/fustion.py

from . import isDevelopment

def checkDevelopment():
    print("isDevelopment = {0}".format(isDevelopment))

./test.py

import manager

if __name__ == "__main__":
    print("isDevelopment = {0}".format(manager.isDevelopment))
    manager.checkDevelopment()

Execute

python3 ./test.py

Output

isDevelopment = True
isDevelopment = True

Question

Are you attempting to execute manager/fusion.py to set the module or do you want it to be part of your executable application? If you simply want to know the value of isDevelopment within the manager module, that can be achieved. If you want an executable function contained in manager explore entry points using setup.py

Kristian
  • 482
  • 2
  • 8
  • Thanks for the investigation: still uncertain what is happening inside my module so it does not work there: but you have established it _should_ in general case. I'll probably award as soon as I can .. and hope to come about what is going on at some point. I gave up for now and made it a module-local variable – WestCoastProjects Feb 05 '20 at 04:52
0

__init__.py is used to initialize the package. According to documentation at https://docs.python.org/3/tutorial/modules.html#packages

Users of the package can import individual modules from the package

You don't import what is in __init__.py, it's run automatically when you do an import.

In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package

As isDevelopment is not a module you cannot import it! If you have another module fusion2.py you could import it with

from . import fusion2

and there you should be able to see isDevelopment.

Javier
  • 1,027
  • 14
  • 23
  • If that import were simply removed: `NameError: name 'isDevelopment' is not defined` Also `pycharm` marks it in red – WestCoastProjects Feb 05 '20 at 03:53
  • Many places in this codebase there are variables being imported: can you please explain further? – WestCoastProjects Feb 05 '20 at 04:01
  • Variables in __init__.py? You should probably expand your question first. It is complicated to answer something we cannot see and evaluate. But from what I see, you don't import variables from __init__.py. – Javier Feb 05 '20 at 04:08
  • I was not focusing on `__init__.py` as being different in that way: will double check if am using variables from it in other places – WestCoastProjects Feb 05 '20 at 04:16
  • Seems should be ok according to this https://stackoverflow.com/a/1383281/1056563 `You should be able to put them in __init__.py. This is done all the time.` – WestCoastProjects Feb 05 '20 at 04:18
  • No. Read the answer you are linking again. The are running "from mypackage import mymodule". They *do* import a module, *not* a constant – Javier Feb 05 '20 at 04:26
  • `from mypackage import MY_CONSTANT` – WestCoastProjects Feb 05 '20 at 04:28