1

I have the following directory structure in my home projects folder.

 |ALL-IN-ONE
    |demo
         |__init__.py
         |__main__.py
    |models
         |grpc
             |allinone_server.py

And I want to import from allinone_server.py a function defined in main.py called images_demo. I have tried

from demo.__main__ import images_demo

It is not working. How can I import it? The function I am trying to import is located inside main.py which is inside demo directory. I am trying to import it from the file allinone_server.py in grpc. I guess I have made my question clear now. Here is the whole tree for the project

├── demo
│   ├── __init__.py
│   ├── __main__.py
│   └── __pycache__
│       ├── __init__.cpython-36.pyc
│       └── main.cpython-36.pyc
├── description
├── environment.yml
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
├── imgs
│   └── 44.jpg
├── info
│   └── exclude
├── __init__.py
├── loggers
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-36.pyc
├── models
│   ├── adience_large1.h5
│   ├── adience_small1.h5
│   ├── AgeModel.json
│   ├── detection_age_gender_large1.h5
│   ├── detection_age_gender_small1.h5
│   ├── detection_age_gender_smile_large1.h5
│   ├── detection_age_gender_smile_small1.h5
│   ├── detection_age_large1.h5
│   ├── detection_age_small1.h5
│   ├── detection_large1.h5
│   ├── detection_small1.h5
│   ├── grpc
│   │   ├── adele_2016.jpg
│   │   ├── allinone_client.py
│   │   ├── all_in_one_pb2_grpc.py
│   │   ├── all_in_one_pb2.py
│   │   ├── all_in_one.proto
│   │   ├── allinone_server.py
│   │   ├── benedict_cumberbatch_2014.png
│   │   ├── cat.png
│   │   ├── classroom_in_tanzania.jpg
│   │   ├── decoded1.py
│   │   ├── decoded.py
│   │   ├── elon_musk_2015.jpg
│   │   ├── laos.jpg
│   │   ├── model_face.jpg
│   │   ├── __pycache__
│   │   │   ├── all_in_one_pb2.cpython-36.pyc
│   │   │   ├── all_in_one_pb2_grpc.cpython-36.pyc
│   │   │   └── decoded.cpython-36.pyc
│   │   ├── sophia.jpg
│   │   ├── test
│   │   │   ├── __init__.py
│   │   │   ├── __pycache__
│   │   │   │   └── __init__.cpython-36.pyc
│   │   │   └── test_images
│   │   │       ├── adele_2016.jpg
│   │   │       ├── benedict_cumberbatch_2014.png
│   │   │       ├── classroom_in_tanzania.jpg
│   │   │       ├── elon_musk_2015.jpg
│   │   │       ├── __init__.py
│   │   │       ├── laos.jpg
│   │   │       ├── model_face.jpg
│   │   │       ├── sophia.jpg
│   │   │       ├── waaah.jpg
│   │   │       ├── woman.jpg
│   │   │       └── zebra_stripes.jpg
│   │   ├── waaah.jpg
│   │   ├── woman.jpg
│   │   └── zebra_stripes.jpg
Samuel Mideksa
  • 423
  • 8
  • 19
  • Possible duplicate of [Import a file from a subdirectory?](https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory) – Alex Oct 25 '18 at 10:56

2 Answers2

1

So you've referred to main.py, but you also have __main__.py in your directory structure. I'll assume that your directory actually contains main.py instead of __main__.py.

To import from levels up in a package, start your import with a period. To import just one function you would use from .main import images_demo

Now, let's start by saying main.py is in grpc/ along with allinone_server.py, then we'll move it to different directories and see how the import changes.

If it were in grpc/ from .main import images_demo

If it were in models/ from ..main import images_demo

If it were in __ALL-IN-ONE/ from ...main import images_demo

If it were in __demo/ from ...__demo.main import images_demo

Every extra period brings you up one level in the hierarchy, then you use the name of the next level down in the target path until you reach where you want to be.

Now let's suppose you wanted to import the whole of main.py. If it were in grpc/ from . import main

If it were in models/ from .. import main

If it were in __ALL-IN-One/ from ... import main

If it were in __demo/ from ...__demo import main

Finally, the dot notation to move up a level only works if the file that uses it is in a package, so this will work fine if at the top level you start your program in a scope outside of this package then use from __ALL-IN-ONE.models.grpc import allinone_server

However, if you run allinone_server.py directly then it will fail to import anything above it as it isn't being imported as part of a package. Try that out, and let me know if that needs better explanation.

Good luck!

MichaelCG8
  • 579
  • 2
  • 14
  • I tried `from ...__demo.main import images_demo` but I couldn't import the module. It is saying `ValueError: attempted relative import beyond top-level package` – Samuel Mideksa Oct 25 '18 at 11:39
  • So that's what happens when you run your file from inside the package, can you comment exactly how you tried to run it? – MichaelCG8 Oct 25 '18 at 19:37
  • I tried to run it from grpc folder using the command `python allinone_server.py` Should I go one directory up and run it? – Samuel Mideksa Oct 26 '18 at 07:20
  • That's the problem, it's what I was trying to describe in my last paragraph but wasn't sure how to word it well, I'll rephrase once we have it working. That's happened because if you run the file directly python doesn't know it is part of a package. Try going to the directory __ABOVE__ ALL-IN-ONE then running `python -c "from ALL-IN-ONE.models.grpc import allinone_server"` There's going to be one more problem here, which is that you can't have a dash in a python name (it would try to do ALL minus IN minus ONE), so rename that to use underscores instead. – MichaelCG8 Oct 26 '18 at 15:24
  • In fact, one more question, are you wanting all of these files to be part of the same package, called ALL_IN_ONE? Or is your package called demo and the models/grpc/allinone_server.py tree a separate thing? – MichaelCG8 Oct 26 '18 at 15:29
  • What I am using right now is trying to copying the file demo into models/grpc and then `from demo.main import images_demo, video_demo, process_video, webcam_demo, video_demo ` by renaming __main__.py to main.py It is working but when I make them in separate directories none of the commands to import the main module are not working. This means it is not going one directory up when I use from .demo and from ..demo – Samuel Mideksa Oct 28 '18 at 10:29
1

You can't import a function from another folder directly and for that you have to use this:

import sys
sys.path.insert(0, "../../demo/")

Another step is to rename __main__ to main.

here is the exact example that worked for me:

The tree:

.
├── demo
│   ├── __init__.py
│   ├── main.py
│   
└── models
    └── grpc
        └── allinone_server.py

main.py:

def images_demo():
    print("hello there")

The calling file(allinone_server.py):

import sys
sys.path.insert(0, "../../demo/")

import main

main.images_demo()
Sadmi
  • 311
  • 2
  • 15
  • How do I import a function defined inside `demo/__main__.py` named `images_demo()`? not the main module? – Samuel Mideksa Oct 25 '18 at 11:48
  • I renamed the function to `images_demo()` to sweet you need. – Sadmi Oct 25 '18 at 12:09
  • I renamed the file __main__.py to main.py and added the line `import main`. When I try to call the function as `main.images_demo()` it says `File "allinone_server.py", line 25, in import main File "../../demo/main.py", line 1, in from demo import load_model,get_layer,selective_search_demo ModuleNotFoundError: No module named 'demo' ` – Samuel Mideksa Oct 25 '18 at 12:46
  • copy and paste the example I provided and try it, and be sure that you are in `grpc` folder. – Sadmi Oct 25 '18 at 13:23
  • Yeah it works but couldn't get it done in my project. May be the tree of the directory structure could help if you take a llook at it. – Samuel Mideksa Oct 25 '18 at 13:43
  • I used exactly your directory tree to avoid conflicts, I think you should give us your code in the question you asked to be clear. – Sadmi Oct 25 '18 at 14:11
  • got it fixed by copying the whole demo directory in the grpc folder which I donot recommend but .s and ..s are not working They are not gonig up the hierarchy from the current directory. – Samuel Mideksa Oct 25 '18 at 14:50