Import module that depends on module with same name - python

My project is based on django and in one app I want to import another module. I installed the module with pip and I'm importing it:
from django.conf import settings
from problematic_package.app.templatetags import test
The issue is that problematic_package is also django based and in test.py they have:
from django.conf import settings
...
Here python will think settings is actually the object from my django project, instead of the imported one.
I can't change the project installed with pip. What other solutions do I have?

Related

Django - ModuleNotFoundError: No module named 'csvexport'

So I'm working on adding an admin function to a Django project so that I can export the model instances to csv. It is all working fine locally whilst using Docker. But when deploying I get this internal server error:
It states that the package is not installed, but when accessing the Django shell (poetry run python3 manage.py shell) and importing the package (from csvexport.actions import csvexport) everything works fine. I'm stuck for quite some time now on this and cant figure out what is going wrong. Anyone had some kind of problem like this with poetry package managing?
settings.py:
INSTALLED_APPS = [
...
'csvexport',
]
model:
from csvexport.actions import csvexport
from django_summernote.admin import SummernoteModelAdmin
class FundAdmin(SummernoteModelAdmin):
...
actions = [csvexport]
manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Looking at the documentation, seems you followed the steps apart from pip install django-admin-csvexport, so my only assumption is that you have not installed the requirement. Can you try adding this to your requirements?

Can't import modules - ImportError: No module named

I've created a new app called engineapp. Inside this app, there is a folder engine which is a Scrapy project.
When I try to import model from storage app inside top.py file, it returns:
from storage.models import TopItem
ImportError: No module named storage.models
Or the similar problem, when I try to import settings of scrapy project:
from engineapp.engine.engine import settings
It returns:
from engineapp.engine.engine import settings
ImportError: No module named engineapp.engine.engine
This is when I run scrapy project from command line.
Both imports created PyCharm itself.
As you can see, I've added __init__() everywhere so python would be able to recognize those files.
Do you know what should I do to be able to import those files?
PyCharm autocomplete relies on different IDE settings. You've marked your realstate_scanner as sources root, so PyCharm can resolve imports for this. From docs:
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
If you can't import some module in python, you should check PATH/PYTHONPATH variables first to make sure python interpreter knows where to find your module.

Import custom module to Django

I'm making a django app and cannot import a custom module inside the views.py file.
I'm trying to import class "Integrate" from auth.py inside modules folder from integrations/views.py
I tried placing init.py inside the app folder and modules folder but still doesn't work.
views.py:
from ..modules.auth import Integrate
Powershell:
from ..modules.auth import Integrate
ValueError: Attempted relative import beyond toplevel package
I do this a lot in my projects. Creating custom modules and importing them.
Try this:
from modules.auth import Integrate
You can use this :
from .models import Integrate
It will be work for use models
I am pretty certain I had this exact problem.
It helps if you make the directory holding the module a 'Sources Root'
You right click on the directory and down the bottom of the pop-up is 'Mark Directory As' option.
AFAIK this adds that directory to the PythonPath so the module in there will be found.

ImportError: cannot import name actions

Trying to run django project, installed all stuff from requirements.txt but still getting error
from django.contrib.admin import ModelAdmin, actions
ImportError: cannot import name actions
Can't find anything useful about it. Does anybody know how to fix it?
UPDATE:
I've found that there is no settings.py file but settings folder instead of it and it contains common.py and production.py and no settings.py. May be that's the problem?
I've tried to run import manually in python shell like this:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chipped.settings.common")
from django.contrib.admin import ModelAdmin, actions
And it told me that there is no postgresql_psycopg2 module. I changed database engine to django.db.backends.sqlite3 and now it works.
i got this error when i upgraded my os x to mavericks
as quux mentioned, you can get real error from python shell and indeed it was _psycopg.so referencing some library that's not available.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.contrib.admin import ModelAdmin, actions
Library not loaded: /usr/local/lib/libpq.5.6.dylib
Referenced from: /Users/nreeves/venvs/gd_api_env/lib/python2.7/site-packages/psycopg2/_psycopg.so
Reason: image not found
so i need to have library link to where _psycopg.so look for.
first I need to locate the lib itself
i installed with brew so my lib is here:
/usr/local/Cellar/postgresql/9.3.4/lib/libpq.5.6.dylib
then I created symlink to /usr/lib (or /usr/local/lib)
sudo ln -s /usr/local/Cellar/postgresql/9.3.4/lib/libpq.5.6.dylib /usr/lib
and all fixed.
This happened to me, turns out I had my database password wrong in settings.py
It happened with me on the one of my projects. I used last version of psycopg2.
I installed to psycopg2==2.5.3 (my other projects use it version) and now it works.
What is actions? You get this error because it isn't present in the admin module

How to import a Django project settings.py Python file from a sub-directory?

I created a sub-directory of my Django project called bin where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project settings.py file that is in a parent directory of bin.
How can I import the settings.py file from a sub-directory of the project?
The code that I use in my command-line script to set into the "Django context" of the project is:
from django.core.management import setup_environ
import settings
setup_environ(settings)
This works fine if the script is in the root directory of my project.
I tried the following two hacks to import the settings.py file and then setup the project:
import os
os.chdir("..")
import sys
sys.path = [str(sys.path[0]) + "/../"] + sys.path
The cruel hack can import settings.py, but then I get the error:
project_module = __import__(project_name, {}, {}, [''])
ValueError: Empty module name
I think your approach may be over-complicating something that Django 1.x provides for you. As long as your project is in your python path, you can set the environment variable DJANGO_SETTINGS_MODULE at the top of your script like so:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
In your command line script where you need to read your settings, simply import the settings module from 'django.conf' as you would do in your application code:
from django.conf import settings
And presto, you have your settings and a Django-enabled environment for your script.
I personally prefer to set my DJANGO_SETTINGS_MODULE using '/usr/bin/env' in a bash script called 'proj_env' so I don't have to repeat it
#!/bin/bash
proj_env="DJANGO_SETTINGS_MODULE=myproject.settings"
/usr/bin/env $proj_env ${*}
With this, now I can run any python script with my Django application in context:
proj_env python -m 'myproject.bin.myscript'
If you use virtualenv, this also gives you a good place to source the activate script.
etc. etc.
This is going one level up from your question, but probably the best solution here is to implement your scripts as custom manage.py (django-admin.py) commands. This gives you all of Django's functionality (including settings) for free with no ugly path-hacking, as well as command-line niceties like options parsing. I've never seen a good reason to write Django-related command-line scripts any other way.
Add the parent directory to your path:
import sys
sys.path.append('../')
import settings
Update from comments:
Don't forget the __init__.py file in
the directory that has your
settings.py – S.Lott
Let's say your project directory is /opt/someProject/`
This has files like:
manage.py
Now you have you may have your sub directory anywhere, does not matter.
Eg. subdirectory could be like:
/opt/someproject/dir1/dir2
Now for you to import your project settings inside /opt/someProject/dir1/dir2
You need to set your PYTHONPATH variable
export PYTHONPATH=/opt/someproject/
Now to import modules from bin
from someproject import bin

Categories