I have been chasing my tail for the last 4 hours here and can't find the solution.
I have the following module/package structure for my project.
.
├── app-cli.py
├── tools
│ ├── __init__.py
│ ├── adapters
│ │ ├── __init__.py
│ │ ├── cli.py
│ │ ├── web.py
│ ├── utils
│ │ ├── __init__.py
│ │ ├── core.py
│ │ │ ├── my_public_method()
│ │ ├── io.py
│ │ │ ├── some_other_public_method()
What I'm trying to do is bundle everything inside utils within the utils name space.
So when I do import tools at the main level, I can access the util functions as:
tools.utils.my_public_method()
tools.utils.some_other_public_method()
Instead of:
tools.utils.core.my_public_method()
tools.utils.io.some_other_public_method()
I have been editing the __init__.py messing around with the levels of imports, attempting to create a shortcut but with no success.
In your __init__.py inside the utils package you can add
from .core import my_public_method
from .io import some_other_public_method
and then you can do:
import tools.utils
tools.utils.my_public_method()
tools.utils.some_other_public_method()
Related
Here's how the directory looks like
├── Caches
│ ├── 11\ Centroids\ representing\ relative\ anchor\ sizes..png
│ ├── 9\ Centroids\ representing\ relative\ anchor\ sizes..png
│ ├── Generated\ anchors\ relative\ to\ sample\ image\ size.png
│ ├── Relative\ width\ and\ height\ for\ 10107\ boxes..png
│ └── data_set_labels.csv
├── Config
│ ├── feature_map.py
│ ├── set_annotation_conf.py
│ └── voc_conf.json
├── Helpers
│ ├── __pycache__
│ │ ├── annotation_parsers.cpython-37.pyc
│ │ └── visual_tools.cpython-37.pyc
│ ├── anchors.py
│ ├── annotation_parsers.py
│ ├── dataset_handlers.py
│ ├── models.py
│ └── visual_tools.py
├── README.md
├── sample_img.png
└── structure_requirements.txt
in dataset_handlers.py I need to import the following:
from ..Config.feature_map import get_feature_map
and in anchors.py I need to import the following:
from .visual_tools import visualization_wrapper
Both imports do resolve in PyCharm or in other words do not have syntax errors however when are run they give the following error:
ImportError: attempted relative import with no known parent package
If I do this:
from visual_tools import visualization_wrapper
the name visual_tools is unresolved however it runs without errors and it imports the requested things
How to be able to make relative imports from .some_module import something without getting errors and being resolved without marking the directory as sources root.
I am learning how to create packages in python and as a part of that exercise I set up the __init__.py files in the submodules. I have a folder structure that looks like the following
├── parent_folder
│ ├── README.txt
│ ├── __init__.py
│ ├── base_estimator.py
│ ├── child_folder_1
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ ├── naming_constants.cpython-36.pyc
│ │ │ └── value_constants.cpython-36.pyc
│ │ ├── naming_constants.py
│ │ └── value_constants.py
│ ├── child_folder_2
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ └── base.cpython-36.pyc
│ │ ├── adult.csv
│ │ ├── axis_fi_nan_treated.csv
│ │ ├── bank_additional_full.csv
│ │ ├── base.py
│ │ ├── boston_house_prices.csv
│ │ ├── breast_cancer.csv
The package is the parent_folder and child_folder_1, child_folder_2 are the sub modules in it
The __init__.py file in the parent_folder looks like this
__all__ = ['child_folder_1', 'child_folder_2', 'base_estimator']
The __init__.py file inside child_folder_2 looks like
from .base import load_pima_diabetes
from .base import load_titanic
from .base import load_iris
from .base import load_data
from .base import load_breast_cancer_wisconsin
from .base import load_boston_house_prices
from .base import load_axis_fi
from .base import load_bank_subscription
from .base import load_adult_income
__all__ = ['load_iris', 'load_boston_house_prices', 'load_bank_subscription', 'load_adult_income',
'load_breast_cancer_wisconsin', 'load_pima_diabetes', 'load_axis_fi', 'load_titanic', 'load_data']
Where .base is a file in child_folder_2 that defines several functions to load datasets. The __init__.py for the child_folder_1 is blank.
Doing this has created a small issue for me- autocomplete. Since I have made these __init__.py files, PyCharm doesn't show anything in autocomplete. I tried importing the child_folder_2 submodule by typing in from parent_folder. but then nothing displays.
Strange thing is that the code runs. It seems like I am missing on something but I can't figure out what. Any ideas on how why this might be occuring?
I have my project tree like .
├── sizer
│ ├── manage.py
│ ├── node
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── models.py
│ │ ├── models.pyc
│ │ ├── node_serializer.py
│ │ ├── node_serializer.pyc
│ │ ├── part_serializer.py
│ │ ├── part_serializer.pyc
│ │ ├── Part_Serializer.pyc
│ │ ├── test.py
│ │ ├── test.pyc
│ │ ├── tests.py
│ │ ├── tests.pyc
│ │ ├── urls.py
│ │ ├── urls.pyc
│ │ ├── views.py
│ │ └── views.pyc
│ ├── requirement.txt
│ ├── sizer
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── settings.py
│ │ ├── settings.pyc
│ │ ├── urls.py
│ │ ├── urls.pyc
│ │ ├── wsgi.py
│ │ └── wsgi.pyc
│ ├── solver
│ │ ├── attrib.py
│ │ ├── attrib.pyc
│ │ ├── cap.py
│ │ ├── cap.pyc
│ │ ├── __init__.py
│ │ ├── node.py
│ │ ├── node.pyc
│ │ ├── nodes1.json
│ │ ├── nodes2.json
│ │ ├── parts.json
│ ├── strings.py
│ ├── strings.pyc
│ └── workload
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── tests.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
I have created node and workload app by manage.py startapp command .
In the above directory structure I copied solver .Now I am importing my node.model under sizer.py file like .
import json
from pulp import *
from attrib import *
from cap import *
from node import *
from wl import *
from sizer.node.models import Part,Node
When I run python solver/sizer.py I am keep on getting
ImportError: No module named node.models
Please help me out what might I am doing wrong here .Spent more then 4 hours still not able to figure out .
Thanks
If your app name is node then your import statement should look like:
from node.models import Part, Node
Note that this requires that you already included node in the INSTALLED_APPS in your settings.py.
There are multiple reasons why an import might fail.
The module is not on the path. To check this print sys.path in your script just before you import the module.
The module is broken and cannot be imported. You can check this by opening a Python console in the same directory as the module and attempting to import. Does that work?
Importing the module results in a CIRCULAR import. This means that the import imports another module that imports another module that imports the original module. This is easy enough to avoid with a little thought and a clear hierarchy.
So, which problem do you have? I have no idea because I can't see the sys.path, and I can't see the code in your files.
What I can see is a bit of a mess. You have multiple modules named 'node'. You have manage.py files at multiple levels. You've included the .pyc files in the output instead of editing them out for the reader. You have so many different modules called 'node', 'sizer' or 'solver' that it must be VERY confusing to figure out which one is being imported at any given time.
Your underlying problem might be that you are trying to work on a project without using source control (git) which means you don't know what changes broke things and you don't feel brave about making big changes because you have no way of stepping back in time if they don't work out.
I have reviewed most of the similar question here.
I'm new to python and I'm using Ubuntu 13.10
The project structure is
├── projecttest
│ ├── api.py
│ ├── controller
│ │ ├── controller.py
│ │ ├── controller.pyc
│ │ ├── init_db.py
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── settings.py
│ │ ├── settings.pyc
│ │ └── extra
│ │ ├── extra.py
│ │ ├── extra.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── __init__.py
│ ├── lib
│ │ └── __init__.py
│ ├── models
│ │ ├── documents.py
│ │ ├── documents.pyc
│ │ └── __init__.py
All the __init__.py files are empty (no hidden characters) and when I'm trying
$ python init_db.py
that has:
from projecttest.models.documents import *
I'm getting:
Traceback (most recent call last):
File "controllers/init_db.py", line 1, in <module>
from projecttest.models.documents import *
ImportError: No module named projecttest.models.documents
You need to specify PYTHONPATH environment variable, it augments the default search path for module files.
It helps to think about PYTHONPATH as an absolute path. If you specify it you may import modules within your program relative to PYTHONPATH.
In your case it would be something like the following line:
PYTHONPATH=/<dir>/<folder>/projecttest/ python init_db.py
Then you may import modules without problems like:
from models.documents import *
When I run an import in my development environment, I seem to get the wrong module.
How do I make sense of this?
$ python -c "import breathe.renderer.rst.doxygen.compound as com; print com"
<module 'breathe.parser.doxygen.compound' from 'breathe/parser/doxygen/compound.pyc'>
The layout of the breathe directory is:
breathe
├── directives.py
├── finder
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── index.py
│ │ └── __init__.py
│ └── __init__.py
├── __init__.py
├── nodes.py
├── parser
│ ├── doxygen
│ │ ├── compound.py
│ │ ├── compoundsuper.py
│ │ ├── index.py
│ │ ├── indexsuper.py
│ │ ├── __init__.py
│ ├── __init__.py
├── process.py
├── renderer
│ ├── __init__.py
│ └── rst
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── domain.py
│ │ ├── filter.py
│ │ ├── index.py
│ │ ├── __init__.py
│ │ └── target.py
│ ├── __init__.py
└── transforms.py
Check your breathe/renderer/rst/__init__.py file. I suspect that you have something like
from breathe.parser import doxygen
What happens is that when a module is imported, the module's __init__.py is executed. Then the next submodule is imported and it's __init__.py is run.
So, in the above situation, you'll get breathe.renderer.rst importing fine, but when running
breathe.renderer.rst.__init__, the breathe.parser.doxygen module is bound to the local name doxygen overriding the doxygen submodule. So you should see something like this:
>>> from breathe.renderer import rst
>>> print(rst.doxygen)
<module 'breathe.parser.doxygen' from 'breathe/parser/doxygen/__init__.pyc'>
Possible solutions:
Use absolute imports in __init__.py
Keep as much code as possible out of __init__.py
Use import inside the functions it's needed rather than globally. (This can also resolve circular import problems)
Note that there is nothing that prohibits code in __init__.py, and several modules in the stdlib do it too. However, because of exactly this sort of issue it is generally considered a good idea to avoid doing it too much.