Python/Django shell won't start - python

One of the great features of Django is that you can open a python interpreter set-up for use with your project. This can be used to analyse objects in a database and allows any python commands to be executed on your project. I find it essential for Django development. It is invoked in the project directory using this command:
$ python manage.py shell
I have just started developing a new project and for some reason the shell does not work. I have had a look online for the error and not found anything. I would greatly appreciate any help on this error:
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/shell.py", line 29, in handle_noargs
shell = IPython.Shell.IPShell(argv=[])
AttributeError: 'module' object has no attribute 'Shell'
Thanks in advance for your help!

It seems like IPython is installed wrongly somehow. Try starting the shell with:
./manage.py shell --plain
to start the standard Python shell, rather than IPython. If that works, then trying removing IPython completely and reinstalling it.

IPython 0.11 has a different API, for which a fix exists in the last Django versions.
For older Django versions, you can use IPython 0.10, which does work:
pip install ipython==0.10

Related

Django 1.8 raises error "module X has no attribute 'run'" after upgrading to Python 3.5

I have a Django project running under Django 1.8.4 that I'm trying to upgrade to latest version of Django. Since I was still using Python 2.7 on a Debian 8 server, I first upgraded my server to Debian 9 and then I upgraded my Django project's virtual environment to Python 3.5.3 (the default version of Python 3 on Debian 9).
After I rebuilt the virtual environment to use Python 3, I ran the command 2to3 -w . in my project's root folder. 2to3 found and fixed 62 problems without any difficulties. I also have 216 unit tests and only two of them failed under Python 3.5.
I then went to my Django website and the site appeared to be working except that the forms that contain pull-down elements show "SomeThingobject" instead of the values from the SomeThing table. I ran a few SQL queries directly against the database and confirmed that my SomeThing lookup table is populated.
Now I want to run a Django queryset command in the Python shell like the following to see why my pulldowns are not showing my column values:
queryset=SomeThing.objects.all()
However, if I activate my virtual environment and try to run the command ./manage.py shell, I get the following error:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 69, in handle
self.run_shell(shell=options['interface'])
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 58, in run_shell
return getattr(self, shell)()
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 41, in ipython
ip()
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 34, in _ipython
from IPython import start_ipython
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/IPython/__init__.py", line 48, in <module>
from .terminal.embed import embed
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/IPython/terminal/embed.py", line 19, in <module>
from IPython.terminal.ipapp import load_default_config
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/IPython/terminal/ipapp.py", line 30, in <module>
from IPython.core.magics import ScriptMagics
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/IPython/core/magics/__init__.py", line 22, in <module>
from .execution import ExecutionMagics
File "/srv/https/my_project.com/venvs/0bc664fa2a7b562d0b7e42a7acb3f095fe5df3cf/lib/python3.5/site-packages/IPython/core/magics/execution.py", line 21, in <module>
import cProfile as profile
File "/usr/lib/python3.5/cProfile.py", line 22, in <module>
run.__doc__ = _pyprofile.run.__doc__
AttributeError: module 'profile' has no attribute 'run'
Now I do have an application named 'profile' in my INSTALLED_APPS. If I try to open the Python shell on my production server which is still using Python 2.7, the shell executes without a problem. It's only having this problem with Python 3.5. What's interesting is that my Django 1.8/Python 2.7 project contains .pyc files but now under Python 3, they're replaced with __pycache__/<filename>.cpython-35.pyc files.
I read the Django document Porting to Python 3 but it doesn't seem to allude to this problem. What do I have to do to make this Django project operate properly under Python 3.5? Is there perhaps something new in Python 3.5 that is having a conflict with my profile application or is it something else?
After much digging around in the Python code I surmised that the problem was being caused by the fact that one of my applications inside the Django project was called "profile" and this was conflicting with a profile module in the newer version of cPython in Python 3.5. After I changed the name of the application to "userprofile" I could start the shell without any problems. These two articles help me change the app's name:
Stackoverflow: How to change the name of a Django app?
How to Change Name of Django Application

Fabric on Python3 Errors out- ImportError: cannot import name 'isMappingType'

I got the following error when I was executing my project that calls fabfile.py. I use a simple subprocess.Popen() to call the fabric task. I am on Python 3.5.1.
File ".bootstrap/_pex/pex.py", line 367, in execute
File ".bootstrap/_pex/pex.py", line 293, in _wrap_coverage
File ".bootstrap/_pex/pex.py", line 325, in _wrap_profiling
File ".bootstrap/_pex/pex.py", line 410, in _execute
File ".bootstrap/_pex/pex.py", line 468, in execute_entry
File ".bootstrap/_pex/pex.py", line 482, in execute_pkg_resources
File ".bootstrap/pkg_resources/__init__.py", line 2297, in resolve
File "/root/.pex/install/Fabric-1.8.3-py3-none-any.whl.ce45adfdb68b47d81b8a5f6b262ce685c93dfc0b/Fabric-1.8.3-py3-none-any.whl/fabric/main.py", line 12, in <module>
from operator import isMappingType
ImportError: cannot import name 'isMappingType'
I've seen many people getting this error, and answers like fabric does not support Python3. But I think that's old news.
Right now, Fabric has Python3 compatibility (specifically, they now
support 2.7 and 3,4+)
Can anyone tell me why is that I'm facing this error even now?
You are using old version of Fabric 1.8.3,
Fabric (2+) has Python 3 compatibility (now support 2.7 and 3.4+)
Therefore, kindly upgrade to the latest version of Fabric Module, considering the upgrade documentation http://docs.fabfile.org/en/latest/upgrading.html

Django 1.8- Stack trace referencing file that doesn't exist

When I run a custom command in Django 1.8.3, I'm getting the following stack trace:
(env)MacBook-Air:backend user$ python manage.py questions
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/Users/user/WeVote/wevote/questions/management/commands/questions.py", line 14, in handle
OSError: [Errno 2] No such file or directory: '/Users/user/WeVote/backend/questions/question_files'
The error is valid, that filepath doesn't exist. I go to look at line 14 in questions.py, except that file doesn't exist. Boo.
As you can see, the file structure is different in referencing questions.py file. As some point, I'm sure that was the file structure. I also wrote this in Djano 1.7 and upgraded to 1.8. However, all the other custom commands (written both before and after the upgrade) are working just fine.
Just as a sanity check, I tried a
cd /Users/user/WeVote/wevote/questions/management/commands/
Nope. This is an issue because when I now edit my properly-located questions.py in the management/commands directory, the changes aren't reflected. It's like it's reading a cached version and location of this file.
Also tried deleting and rebuilding both virtualenv and database. No love on either. I'm sure it's something simple I'm overlooking, but I'm out of ideas.
Reference for anyone coming afterwards: The comments from yuvi and Paul Collingwood above succeeded in solving my issue. I ran
find . -name '*.pyc' -delete
and the next time I ran questions.py everything worked well. Thanks both for your comments.

Change requires_model_validation in a project

I've uploaded from django 1.6 to django 1.7 and from python 3.2.3 to python 3.4.1.
Suddenly when I try to run the makemessages command to catch the translating messages I find this error:
django.core.exceptions.ImproperlyConfigured: Command Command defines both "requires_model_validation" and "requires_system_checks", which is illegal. Use only "requires_system_checks".
I've googled it and I don't find where or what I should edit. In fact I've never used this parameter so...
Any idea?
The traceback
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 238, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 42, in load_command_class
return module.Command()
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/base.py", line 228, in __init__
'"requires_system_checks".' % self.__class__.__name__)
django.core.exceptions.ImproperlyConfigured: Command Command defines both "requires_model_validation" and "requires_system_checks", which is illegal. Use only "requires_system_checks".
Finally I've found the answer and it's not related with django core at all. I was not using any command. However one of my installed apps was using it. In this case it's django vinaigrette.
I'll upload the patch ASAP to port this app to django 1.7.
Regards.
Thanks to #Paul Pepper for pointing me in the right direction. For me it was Django devserver causing the issue and updating to the latest version fixed it for me:
pip install -e git://github.com/dcramer/django-devserver.git#HEAD#egg=django-devserver
HEAD was commit e6c882fc11fba013b85d37f04db8f11b531eda9a at the time of this writing.

Django manage.py custom command on Heroku

I am trying to run a cron job with a Heroku hosted website. I am using a Django back-end. I have written a custom manage.py command that works locally (status_crawl.py). I have seen a few guides and posts on how to fix this, but the suggestions did not work (such as Running Django custom manage.py task on Heroku - Importing Issues and this guide).
Even weirder is that it is recognizing the other commands. (And I managed to get the basic task to work by using runscript from the django-extensions framework). It is still bothering me though.
My basic file structure likes like:
likes:
...
management:
__init__.py
commands:
__init__.py
status_crawl.py
The traceback is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named commands.status_crawl
The other weird thing is that when I run 'heroku run python manage.py help' the command is showing up as an option. It's just not running when I try to run the script myself using the manage.py command.
Thanks!
Probably it's related to your PYTHONPATH variable on Heroku. This variable on Heroku may differ from your local PYTHONPATH. Try to import from the root of your project, like:
from likes.management.commands import status_crawl

Categories