ImportError: cannot import name actions - python

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

Related

Django Trouble: django.setup() throws "ImportError: No module named 'project_name'"

I've been having an issue that I believe can be solved one of 2 ways. We have a django project I am trying to write some iPython notebooks for teaching purposes and have them in a tutorials directory that's one below the root of the project (i.e. ~/tutorials).
In the beginning of the notebooks I need to import several modules but I encountered a similar problem to the one detailed in this stack overflow question: Relative imports in Python 3 , (python couldn't find the paths, even though it could when I ran the notebooks from the root directory)
My solution was to specify the absolute path to the root of the directory in a path variable and append it like the following:
import sys
path = 'Users/greg/project_name'
sys.path.append(path)
That worked and I have been able to make the imports. However, the problem I am encountering now is that when I run a function that calls get on a Django model like so:
Platform.objects.get(name='platform_name').platform_levels.get(db_name=level).id
it raises an error from the python3.4/site-packages/django/apps/registry.py file saying:
AppRegistryNotReady: Models aren't loaded yet.
Does anybody have any insight into why they aren't being loaded and what I need to do differently? Please let me know if there's other information that I should provide to help you answer this question. Thanks!
Edit: The suggestion by #albar led to a new error which I think is the root of all the issues. When I go to run django.setup(), it raises an error on this line:
mod = importlib.import_module(self.SETTINGS_MODULE)
saying:
ImportError: No module named 'project_name'
Does it need to be able to import the entire project as a module and if so, how do I tell it how to find it?
You have to setup Django in your script:
import django
django.setup()
EDIT: Before that, you have to define your settings module:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

python: Django import fails asking me to define DJANGO_SETTINGS_MODULE

I am newbee in Django.I finished the Django Tutorial once from the official djangoproject site.Not saying I know everything now, but everything worked perfectly for me till I was referring that website.
I started referring to the Practical Django projects book and somewhere in my models I tried to import :
from django.contrib.auth.models import User
gives me an import error :
ImportError Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined
`
What is really going on here ? I have been successfully importing all the packages till now then why this sudden error ?
How are you running the code that gives you the error? If you are importing the models.py in the shell, use the Django shell command
./manage.py shell
If you are trying the import in another script, you have to set the environment variable DJANGO_SETTINGS_MODULE manually.
import os
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
This requires myproject to be in your python path.
See this similar Stack Overflow question for futher discussion.

Can't import my own modules in Python

I'm having a hard time understanding how module importing works in Python (I've never done it in any other language before either).
Let's say I have:
myapp/__init__.py
myapp/myapp/myapp.py
myapp/myapp/SomeObject.py
myapp/tests/TestCase.py
Now I'm trying to get something like this:
myapp.py
===================
from myapp import SomeObject
# stuff ...
TestCase.py
===================
from myapp import SomeObject
# some tests on SomeObject
However, I'm definitely doing something wrong as Python can't see that myapp is a module:
ImportError: No module named myapp
In your particular case it looks like you're trying to import SomeObject from the myapp.py and TestCase.py scripts. From myapp.py, do
import SomeObject
since it is in the same folder. For TestCase.py, do
from ..myapp import SomeObject
However, this will work only if you are importing TestCase from the package. If you want to directly run python TestCase.py, you would have to mess with your path. This can be done within Python:
import sys
sys.path.append("..")
from myapp import SomeObject
though that is generally not recommended.
In general, if you want other people to use your Python package, you should use distutils to create a setup script. That way, anyone can install your package easily using a command like python setup.py install and it will be available everywhere on their machine. If you're serious about the package, you could even add it to the Python Package Index, PyPI.
The function import looks for files into your PYTHONPATH env. variable and your local directory. So you can either put all your files in the same directory, or export the path typing into a terminal::
export PYTHONPATH="$PYTHONPATH:/path_to_myapp/myapp/myapp/"
exporting path is a good way. Another way is to add a .pth to your site-packages location.
On my mac my python keeps site-packages in /Library/Python shown below
/Library/Python/2.7/site-packages
I created a file called awesome.pth at /Library/Python/2.7/site-packages/awesome.pth and in the file put the following path that references my awesome modules
/opt/awesome/custom_python_modules
You can try
from myapp.myapp import SomeObject
because your project name is the same as the myapp.py which makes it search the project document first
You need to have
__init__.py
in all the folders that have code you need to interact with.
You also need to specify the top folder name of your project in every import even if the file you tried to import is at the same level.
In your first myapp directory ,u can add a setup.py file and add two python code in setup.py
from setuptools import setup
setup(name='myapp')
in your first myapp directory in commandline , use pip install -e . to install the package
pip install on Windows 10 defaults to installing in 'Program Files/PythonXX/Lib/site-packages' which is a directory that requires administrative privileges. So I fixed my issue by running pip install as Administrator (you have to open command prompt as administrator even if you are logged in with an admin account). Also, it is safer to call pip from python.
e.g.
python -m pip install <package-name>
instead of
pip install <package-name>
In my case it was Windows vs Python surprise, despite Windows filenames are not case sensitive, Python import is. So if you have Stuff.py file you need to import this name as-is.
let's say i write a module
import os
my_home_dir=os.environ['HOME'] // in windows 'HOMEPATH'
file_abs_path=os.path.join(my_home_dir,"my_module.py")
with open(file_abs_path,"w") as f:
f.write("print('I am loaded successfully')")
import importlib
importlib.util.find_spec('my_module') ==> cannot find
we have to tell python where to look for the module. we have to add our path to the sys.path
import sys
sys.path.append(file_abs_path)
now importlib.util.find_spec('my_module') returns:
ModuleSpec(name='my_module', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7fa40143e8e0>, origin='/Users/name/my_module.py')
we created our module, we informed python its path, now we should be able to import it
import my_module
//I am loaded successfully
This worked for me:
from .myapp import SomeObject
The . signifies that it will search any local modules from the parent module.
Short Answer:
python -m ParentPackage.Submodule
Executing the required file via module flag worked for me. Lets say we got a typical directory structure as below:
my_project:
| Core
->myScript.py
| Utils
->helpers.py
configs.py
Now if you want to run a file inside a directory, that has imports from other modules, all you need to do is like below:
python -m Core.myscript
PS: You gotta use dot notation to refer the submodules(Files/scripts you want to execute). Also I used python3.9+. So I didnt require neither any init.py nor any sys path append statements.
Hope that helps! Happy Coding!
If you use Anaconda you can do:
conda develop /Path/To/Your/Modules
from the Shell and it will write your path into a conda.pth file into the standard directory for 3rd party modules (site-packages in my case).
If you are using the IPython Console, make sure your IDE (e.g., spyder) is pointing to the right working directory (i.e., your project folder)
Besides the suggested solutions like the accepted answer, I had the same problem in Pycharm, and I didn't want to modify imports like the relative addressing suggested above.
I finally found out that if I mark my src/ (root directory of my python codes) as the source in Interpreter settings, the issue will be resolved.

Django settings.py Error: Import by filename is not supported

I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.

Problem with Python modules

I'm uploading my first Django app to my Dreamhost server. My app uses xlwt package and since I can't install it in the default location ( /usr/lib/python2.3/site-packages/xlwt ), I installed it on another location by:
python setup.py install --home=$HOME
Then xlwt is installed here:
/home/myuser/lib/python/xlwt/
After that, I add this folder to de env var PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/home/myuser/lib/python
... And in a python promt I can do this (without problems)
import xlwt
... But if I do the same thing in my app code, I have the follow error:
Could not import ISI.restaurante.views. Error was: No module named xlwt
[where ISI.restaurante.views is my code where I do the import]
Could u help me? Thanks!
PYTHONPATH may only be set when you run from the shell, you can set path programatically from python using
import sys
sys.path.append('/home/myuser/lib/python')

Categories