Python importing issue - python

I have a module(Executive.py) that I am trying to import into another module I am working on (ExecutiveTest.py). The directory structure is like so:
src/
common/
python/
Executive.py
tests/
ExecutiveTest.py
In ExecutiveTest.py, I have the following line:
from common.Executive import Executive
I get an error saying:ImportError: No module named common.Executive
How do I correct this import error?

You have to have an __init__.py file in the root of your package (it can be empty). Also, your module hierarchy has to reflect the directory structure, so python and tests should be part of the import as well.

I found a similar post here. it looks like you can define the path that python refers to when looking for stuff to import. Something like:
sys.path.append( )

Related

importing python module using relative name

I'm having difficultly with important a python module from another folder. Here's how my folder looks currently
foldername/
__init__.py
A/
__init__.py
spam.py
grok.py
B/
__init__.py
foo.py
I'm trying to import the functions and classes from the grok.py file into the foo.py in B. This is how my foo.py looks like
from ..A.spam import func
However, I get the following error:
ValueError: attempted relative import beyond top-level package
Could somebody help me? I don't understand where I'm going wrong
You can't use '..' like you do on the command line. You have to add your 'A' folder to your Python path. You can use sys.path.append('/dir/of/A') and then from A.spam import func
Instead of using sys.path you could also add a *.pth-file to your python or anaconda "site-packages"-folder which contains the path to the folder "A".
Import via from A.spam import func as #bikemule already proposed.

How to import from a python package which is in a sibling directory?

I am new to python and i am having some issues with packages and imports.
My structure is as follows:
src/
base/
packcore/
__init__.py
utils/
__init__.py
util_1.py
util_2.py
helpers/
__init__.py
helper_1.py
helper_2.py
some_script.py
app.py
packcore is an external package that has been installed using pip and put into the --target=base.
some of the helpers in the packcore uses some of the utils.
From app.py i want to be able to import a helper/util.
But when i use from base.packcore.utils import some_util i get an error saying that no module named packcore from inside the helper/util
and if i do from packcore.utils import some_util i get an error no module named packcorefrom theapp.py`
help would be much appreciated :)
If you add an __init__.py to base/, you can make it a Python package to import from. You also need to make the parent a package (Which is currently called src) so it is actually a sibling module, rather than many isolated modules.
From there, you can either do an absolute import from the main package:
from src.base.packcore.helpers import helper_1
Or relative (Assuming you are in some_script.py or app.py):
from .base.packcore.helpers import helper_1

ImportError: No module named test.meetinghandler

Hey friends i set my python path as
/home/rohit/test
Here is the structure my test folder
test/
__init__.py
meetinghandler/
__init__.py
meetinghandler.py
db/
__init__.py
models.py
setting.py
manage.py
My problem is when i am trying to import
from test.meetinghandler import meetinghandler
from models.py
i am getting error i.e;
ImportError: No module named test.meetinghandler
please help me out what i am doing wrong .
from test import meetinghandler looks for a test module, which it will not find in your python path.
The reason? You added /home/rohit/test/ to your python path, but that directory doesn't itself contain a test directory :-) Python looks for modules inside the path, that's why.
In your case you'd have to add your home dir to your python path to be able to find it. (Or better, google for virtualenv and so).
Try debugging from the console.
See if the import test works without error. If so, likely there is some error statement in your meetinghandler.py that is not loding the file.
If the import test itself fails on the console, see if the test is in the python path (i.e, as a part of the folder that is present in the settings.INSTALLED_APPS)

Python absolute import in module fails

I have a project which looks like this:
my_project/
__init__.py -- empty
run.py
datacheck/
__init__.py -- empty
datacheck.py -- containing class DataCheck(object)
config.py -- containing BusinessConfig(object)
business.py -- containing class BusinessCheck(DataCheck)
My PYTHONPATH is configured to have /my_project in it.
In run.py, I have the following code:
from datacheck.business import BusinessCheck
business = BusinessCheck()
business.check_data()
In business.py, I have the following imports that fail:
from datacheck.config import BusinessConfig
from datacheck.datacheck import DataCheck
A relative import like from .config import BusinessConfig works - however I've read in numerous threads that an absolute import is preferred.
To do a simple test, I also created the following:
myproject/
__init__.py -- empty
run_test.py
test/
__init__.py -- empty
test1.py -- containing class Test1(object)
test2.py -- containing class Test2(Test1)
run_test.py imports and runs the Test2 class, this didn't fail.
It left me a bit flabbergasted, I don't understand why my absolute imports in datacheck are not working - can anyone explain?
You should prefer absolute imports if your module can be used as __main__, as explained in the documentation. If not, relative imports are fine.
These imports fail, because your package datacheck contains a module datacheck (same name). When looking up the name, Python implicitly looks inside the package first. There, it finds the module datacheck. This module, however, does not contain anything with the name config, so the import fails.
If you want to use absolute imports, move all the stuff from the module datacheck into the __init__.py of the package, or rename it.
I know this is many years later, but for the sake of others searching here, I was able to resolve a similar problem with this bit of code:
from __future__ import absolute_import
After that, absolute imports worked fine in Python 2.6 and 2.7.

python packaging for relative imports

First off all: I'm sorry, I know there has been lots of question about relative imports, but I just didn't find a solution. If possible I would like to use the following directory layout:
myClass/
__init__.py
test/
demo.py
benchmark.py
specs.py
src/
__init__.py
myClass.py
Now my questions are:
How do the test files from within the package properly import myClass.py?
How would you import the package from outside, assuming you take myClass as submodule in libs/myClass or include/myClass?
So far I couldn't find an elegant solution for this. From what I understand Guido's Decision it should be possible to do from ..src import myClass but this will error:
ValueError: Attempted relative import in non-package
Which looks as it doesn't treat myClass as packages. Reading the docs:
The __init__.py files are required to make Python treat the directories as containing packages;
It seems I'm missing something that specifies where the scripts of the package are, should I use .pth ?
ValueError: Attempted relative import in non-package
Means you attempt to use relative import in the module which is not package. Its problem with the file which has this from ... import statement, and not the file which you are trying to import.
So if you are doing relative imports in your tests, for example, you should make your tests to be part of your package. This means
Adding __init__.py to test/
Running them from some outside script, like nosetests
If you run something as python myClass/test/demo.py, relative imports will not work too since you are running demo module not as package. Relative imports require that the module which uses them is being imported itself either as package module, from myClass.test.demo import blabla, or with relative import.
After hours of searching last night I found the answer to relative imports in python!! Or an easy solution at the very least. The best way to fix this is to have the modules called from another module. So say you want demo.py to import myClass.py. In the myClass folder at the root of the sub-packages they need to have a file that calls the other two. From what I gather the working directory is always considered __main__ so if you test the import from demo.py with the demo.py script, you will receive that error. To illustrate:
Folder hierarchy:
myClass/
main.py #arbitrary name, can be anything
test/
__init__.py
demo.py
src/
__init__.py
myClass.py
myClass.py:
def randomMaths(x):
a = x * 2
y = x * a
return y
demo.py:
from ..src import myClass
def printer():
print(myClass.randomMaths(42))
main.py:
import test.demo
demo.printer()
If you run demo.py in the interpreter, you will generate an error, but running main.py will not. It's a little convoluted, but it works :D
Intra-package-references describes how to myClass from test/*. To import the package from outside, you should add its path to PYTHONPATH environment variable before running the importer application, or to sys.path list in the code before importing it.
Why from ..src import myClass fails: probably, src is not a python package, you cannot import from there. You should add it to python path as described above.

Categories