0

I copied python project B to my project A as a model. But B's include path is just based on B. So how can I call B's function from A. For example,

.
└── mypackage A
    ├── subpackage_1
    │   ├── test11.py
    │   └── test12.py
    ├── subpackage_2
    │   ├── test21.py
    │   └── test22.py
    └── subpackage B
        ├── test31.py
        └── test32.py

test31.py may include test32.py by

import test32

But from A's prospective, I should include it by

import B.test32

In fact, B is more complex than this example. How can I refactor it?

3 Answers 3

1

first make module using __init__.py in B then try this:

from B.test32 import 'function Name'

and then call it.

Sign up to request clarification or add additional context in comments.

Comments

0

As @Wàlid Bachri said, you should start placing __init__.py files, but it is a bit more complex than than. You must put those __init__.py files, in each directory. So it would Look something like this.

.
└── mypackage A
    ├── __init__.py # here under A
    ├── subpackage_1
    |   ├── __init__.py
    │   ├── test11.py
    │   └── test12.py
    ├── subpackage_2
    |   ├── __init__.py # under package 2
    │   ├── test21.py
    │   └── test22.py
    └── subpackage B 
        ├── __init__.py # under package B
        ├── test31.py
        └── test32.py

In each of the __init__.py files, you need to import all the files in the same directory. So for example in subpackage B you need to do this.

#/subpackage B/__init__.py
from . import test31
from . import test32

in mypackage A, where there are no files, just directories, you also do the same thing (from . import subpackage_2 etc).

If I suppose that mypackage A is the "main" package (as can be seen by your diargam), so it is not a submodule, to run any file you will need to execute the following.

First cd to the parent directory of mypackage A and then

# Suppose you want to execute /subpackage_1/test11
python -m mypackage_A.subpackage_1.test11 # WARNING mypackage A should have no whitespace

You may get a RuntimeWarning about your sys.modules being modified, but I can assure you, from experience, that you can safely ignore it. This is how python modules for pip are normally done, to ensure that pushing to production is easy.

EDIT : Also note that in all of your files, you should switch to package imports for relative imports, so you must use the dot syntax.

from . import test32 # this will import .test32

if you were to instead to import test32 from test31, the interpeter would try to search for a global package named test32 and not look in the same directory.

Comments

0

Simply add an empty python file called __init__.py to your B project to make Python treat this project as a normal python module.

2 Comments

No, it doesn't work. I'm afraid about the import path.
Try this from B import test32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.