ImportError: no mudule named ini - python

I have a directory structure like
akom
DB
DBinit.py
__init__.py
server.py
Configuration(dir)
urls.py
settings.py
manage.py
ini.py
__init__.py
I have set the PYTHONPATH in my .bashrc as export PYTHONPATH=$PYTHONPATH:/home/dir1/dir2/
Inside dir2 akom reside.
I am importing the ini.py on server.py like import ini but i am getting the error that
ImportError: No module named ini
Please tell me where i am going wrong .?

Since akom is a package itself, you need to reference modules inside that package by the package name first - e.g. by using import akom.ini instead of import ini.
Update: Reading above, it's somewhat unclear if ini.py is in the Configuration directory - if it is you will need to do import akom.Configuration.ini.

Related

Django: no module named http_utils

I created a new utils package and an http_utils file with some decorators and HTTP utility functions in there. I imported them wherever I am using them and the IDE reports no errors, and I also added the utils module to the INSTALLED_APPS list.
However, when launching the server I am getting an import error:
ImportError: No module named http_utils
What am I missing? What else do I need to do to register a new module?
Make sure the package is correct (Include init.py file).
Make sure there are no other utils files in the same directory level. That is if you are importing from utils import http_utils from views.py, there should not be a utils.py in the same folder. Conflict occurs because of that.
You dont have to include the folder in the INSTALLED_APP settings. Because the utils folder is a package and should be available for importing
As stared by Arundas above, since there is a utils.py file I suggest renaming the module to something such as utilities, and also make sure you have a __init__.py file in that directory.
from utilities.http_utils import class_name

Importing from parent directory gets error

The project structure on my local machine is setup like this:
python/
__init__.py
readText.py
testing/
__init__.py
removeDuplicates.py
In removeDuplicates.py I am trying to import as follows:
from python import readText
This gives: ImportError: No module name 'python'
My init.py in both folders are blank by the way.
You need the parent directory of your python subdirectory to be present in sys.path. If you execute your script from that directory, the import should work. But the easiest way to do this is to export the environment variable PYTHONPATH.
You want to import something from the parent directory, use
from .. import readText
see relative imports:
https://docs.python.org/2.5/whatsnew/pep-328.html

Django importing another file from another package

I have the following folder structure
app/
app/helpers/
app/helpers/methodhelper.py
app/methods/
app/methods/method.py
and I'm trying to import a function from methodhelper.py inside method.py
so I tried the following:
import app.helpers.methodhelper
OR
from app.helpers.methodhelper import function1
OR
import helpers.methodhelper
and I get:
"No module named app.helpers.methodhelper"
Important to note: helpers/__init__.py already exists
How should this be done ?
Your Django project's default path is in the root directory of the project (where the manage.py file is). You can either add the sub directories below that to your PYTHONPATH (easily done by appending to sys.path) or you can import that function using the full module path:
from projectname.app.helpers.methodhelper import function1
When I start a Django project, I always add
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
to my settings.py. This path looks similar to /home/kyle/django_project_name/. Inside that directly is manage.py.
From there, also in my settings.py, I include:
sys.path.append(os.path.join(PROJECT_ROOT, 'django_project_name'))
This makes my apps importable without the need to include my project name in the module path.
you need to add the module in settings.py file

Cannot import modules of parent packages inside child packages

I have a parent package that has 2 child packages. It looks like this
backend
__init__.py
conf.py
db.py
connections.py
/api
__init__.py
register.py
api.py
/scheduled
__init__.py
helpers.py
All the __init__.py files are empty.
The code in backend/connections.py and backend/conf.py is being used by modules in both packages api and scheduled.
in register.py i have code like
from backend.conf import *
from backend.connections import *
Now when i do python register.py
i get this error
ImportError: No module named backend.conf
Also when i changed from backend.conf import * to from ..conf import * or from .. import conf i get this error
ValueError: Attempted relative import in non-package
What i understand by the above error is that python is not treating the above folders as packages. But i have __init__.py in all the folders. What is wrong?
When you run python register.py, your backend/register.py file is used as the __main__ module of the program, rather than as a module within the backend package. Further more, the Python import path will not automatically include the directory containing the backend directory, which is probably the cause of your problems.
One option that might work is to run your program as python -m backend.register from the top level directory of your project (or set PYTHONPATH so this module can be found). This will search for the script on the normal import path, and then run it as the main program.

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)

Categories