No module named django urls for any django-admin command - python

I'm having trouble running Django on my company's server. When I try to start a project, like django-admin startproject newproject, it gives me:
File "/opt/packages/anaconda/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
from django.urls import set_script_prefix
ImportError: No module named urls
When I run python and import django; django.VERSION, I get (1, 10, 2, u'final', 0). (I recently downgraded to 1.9.5 to match my company's version).
And of course the company runs 1.9.5 from which I checked from pip list. Shouldn't I get 1.9.5 also when I call for the version in python?
Here is my profile also:
python='/opt/packages/anaconda/bin/python'
export PATH=/opt/packages/anaconda/bin:$PATH
export LD_LIBRARY_PATH=/opt/packages/anaconda/lib:$LD_LIBRARY_PATH
export TCL_LIBRARY=/opt/packages/anaconda/lib/tcl8.5

Related

Bash script gives psycopg2 error: symbol not found in flat namespace (_PQbackendPID)?

I am using Apple M1. Here is the bash script I wrote.
#!/bin/sh
# Open Activity Tracker website
python -m webbrowser http://127.0.0.1:8000/
# Activate virtual environment
. dj-env/bin/activate
# Navigate to Activity Tracker project folder
cd dj_activity_tracker
# Run Django local development server
python manage.py runserver
When you run manually are you doing that in the same virtualenv? Yes.
Are you doing the installs into the virtualenv? Yes.
What version of psycopg? Within my virtual environment, I run "pip show psycopg2-binary". It outputs "Version: 2.9.5".
When I run these commands on my terminal manually without the script, it works. However, when I use this script, I get the error:
File "/Users/doge/Desktop/dj-env/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 28, in \<module\>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/doge/Desktop/dj-env/lib/python3.11/site-packages/psycopg2/\_psycopg.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace (\_PQbackendPID)
Things I have tried:
Uninstall/reinstall psycopg2
brew install postgresql
I am stumped on how this is working manually but not when I run the script. What could be the issue? I appreciate your support.

"Django deprecated" error in Django-Admin

When I am using django-admin startproject api then it is opening file name django-admin.py and the code looks like this :
#!C:\Users\Administrator\Desktop\api\venv\Scripts\python.exe
## When the django-admin.py deprecation ends, remove this script.
import warnings
from django.core import management
try:
from django.utils.deprecation import RemovedInDjango40Warning
except ImportError:
raise ImportError(
'django-admin.py was deprecated in Django 3.1 and removed in Django '
'4.0. Please manually remove this script from your virtual environment '
'and use django-admin instead.'
)
if __name__ == "__main__":
warnings.warn(
'django-admin.py is deprecated in favor of django-admin.',
RemovedInDjango40Warning,
)
management.execute_from_command_line()
I am using virtual environment. I am not getting how to get rid of it.
Short answer:
Use django-admin.exe instead of django-admin because django-admin.py is deprecated.
Example:
django-admin.exe startproject NEW_PROJECT_NAME
django-admin.exe startapp NEW_APP_NAME
Long answer:
If you use the command "where django-admin" (Windows) / "whereis django-admin" (Linux), you will see that there are probably two shortcuts.
In my case:
C:\Program Files\Python39\Scripts\django-admin.exe
C:\Program Files\Python39\Scripts\django-admin.py
I suppose that using an .exe extension file is faster than using the .py python extension file.
There may be another solution, I haven't tried to delete the file with "py" extension (django-admin.py), I just found that the fastest solution was to use "django-admin.exe startproject PROJECT_NAME", and so on.
The most straightforward way is probably to uninstall and reinstall Django in your virtual environment, if you're okay using the latest version of 3.2:
pip uninstall Django
pip install 'Django<4'

I have a Django Question regarding the command line regarding running python manage.py test

I got this error when I was running the command python manage.py test in the absolute pathname
C:\Users\vitorfs\Development\myproject\myproject on the command line which I opened by selecting Run as Admin on Windows. In boards.py, I have the following
from django.core.urlresolvers import reverse
from django.test import TestCase
class HomeTests(TestCase):
def test_home_view_status_code(self):
url = reverse('home')
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
This is the errors I'm getting:
1. Failed to import test module: board.tests. 2. ModuleNotFoundError: No module named django.core.urls
I am having trouble fixing the error. I followed the tutorial on Django at https://simpleisbetterthancomplex.com/series/2017/09/11/a-complete-beginners-guide-to-django-part-2.html
Can you show me what this means and help me fix this error?
Just to let you know, I'm learning Python and Django for fun.
Reinstalling django is only option because your core utilities are broken
Follow These Steps:
In Your Command prompt- pip uninstall django
In Your Command prompt- pip install django==2.7 # Assuming That You've django 2.7
Maybe This Would Help, Let Me Know If There Are Any More Problems I'am up for you!

Django Black Not Installing Properly in Pipenv environment

I received an error after installing Django Black. I ran pipenv install black --pre. Then when I ran manage.py runserver, I received the error below. Note that I am running Windows 10.
from custom_storages import MediaStorage
File "C:\Users\dgold2\Documents\py\ibankai\src\custom_storages.py", line 2, in <module>
from storages.backends.s3boto3 import S3Boto3Storage
File "C:\Users\dgold2\Documents\py\ibankai\src\.venv\lib\site-packages\storages\backends\s3boto3.py", line 18, in <module>
from django.utils.six.moves.urllib import parse as urlparse
ModuleNotFoundError: No module named 'django.utils.six'
I ran into this when I was using a template for Django + Vue on Heroku, the only difference being that in my case the error was thrown by whitenoise, a static files middleware, rather than s3boto3. The root cause is that my Pipfile specified Django = "*" but whitenoise = "==4.0". So pipenv was grabbing the most recent available Django, which was 3.0, but whitenoise 4 was trying to import from django.utils.six (like your s3boto3 package), which seems to have been deprecated in Django 3. (Compare 2.2, 3.0).
See if there's a s3boto3 version specifier in your Pipfile that you can update, or alternatively specify Django ="2.2" if you're willing to hold off on Django 3.

How to resolve cannot import name pages (django/wagtail)

Recently I have been trying to get wagtail to work with my existing Django application. I was experiencing an error: -
ImportError: No module named wagtail
unable to load app 0 (mountpoint='') (callable not found or import error)
After much troubleshooting I managed to fix this, by copying the folder wagtail from:
/usr/local/lib/python2.7/dist-packages/
into here
/opt/django/src/
Having resolved this error, I received another about a different module, and another... each time I copied the folder from /usr/local/lib/python2.7/dist-packages/ into /opt/django/src/ and it eventually resolved the issues I was having and uWSGI started.
Now when I access the homepage of my app, I receive this error
ImportError at /
cannot import name pages
Request Method: GET
Request URL: http://example.com
Django Version: 1.9
Exception Type: ImportError
Exception Value:
cannot import name pages
Exception Location: ./wagtail/wagtailadmin/urls/__init__.py in <module>, line 4
Python Executable: /usr/local/bin/uwsgi
Python Version: 2.7.3
Python Path:
['.',
'',
'/opt/django/src',
'/root/.python',
'/opt/django/env/lib/python2.7',
'/opt/django/env/lib/python2.7/plat-linux2',
'/opt/django/env/lib/python2.7/lib-tk',
'/opt/django/env/lib/python2.7/lib-old',
'/opt/django/env/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/opt/django/env/local/lib/python2.7/site-packages',
'/opt/django/env/lib/python2.7/site-packages']
I have checked the file referenced (./wagtail/wagtailadmin/urls/__init__.py) and it looks like this:
from django.conf.urls import url, include
from django.views.decorators.cache import cache_control
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
from wagtail.wagtailadmin.urls import collections as wagtailadmin_collections_urls
from wagtail.wagtailadmin.urls import password_reset as wagtailadmin_password_reset_urls
from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar
from wagtail.wagtailadmin.api import urls as api_urls
from wagtail.wagtailcore import hooks
from wagtail.utils.urlpatterns import decorate_urlpatterns
from wagtail.wagtailadmin.decorators import require_admin_access
The offending line is the first wagtail 'from' ...
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
How can I resolve this error?
I've ran a few tests on wagtail setup. I made each test in a fresh Ubuntu 14 install (each test in a new workspace on Cloud9 IDE).
Test 1: Straight python 2.7
sudo pip install wagtail
ERRO: error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Comment: Lots of x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-2.7/libImaging/codec_fd.o: No such file or directory errors were output.
Didn't work.
Test 2 - Python 2.7 with virtualenv
sudo pip install virtualenv
virtualenv env
source env/bin/activate
Now from their github https://github.com/wagtail/wagtail
pip install wagtail
out: Successfully installed Django-1.10.7 Pillow-4.1.1 Unidecode-0.4.20 Willow-0.4 beautifulsoup4-4.5.3 django-modelcluster-3.1 django-taggit-0.22.1 django-treebeard-4.1.0 djangorestframework-3.6.2 html5lib-0.999999999 olefile-0.44 pytz-2017.2 requests-2.13.0 wagtail-1.9.1 webencodings-0.5.1
wagtail start mysite
cd mysite
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
I had to edit the settings/dev.py and include ALLOWED_HOSTS = ['*'] because I'm on cloud 9. Also needed the $IP:$PORT after runserver. WORKED!
Test3 - Python 3.4 with virtualenv
sudo pip3 install virtualenv
virtualenv -p python3 env
source env/bin/activate
pip install wagtail
Successfully installed Django-1.10.7 Pillow-4.1.1 Unidecode-0.4.20 Willow-0.4 beautifulsoup4-4.5.3 django-modelcluster-3.1 django-taggit-0.22.1 django-treebeard-4.1.0 djangorestframework-3.6.2 html5lib-0.999999999 olefile-0.44 pytz-2017.2 requests-2.13.0 wagtail-1.9.1 webencodings-0.5.1
Rest of the command are the same. And it WORKED!
Test 4 - Python 3.4 no virtualenv
sudo pip3 install wagtail
out: Successfully installed wagtail django-taggit requests Unidecode Django djangorestframework Pillow beautifulsoup4 Willow django-modelcluster django-treebeard olefile pytz
wagtail start mysite
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py runserver
EDIT: Made a mistake. Should've run with python3, not python in this case. Did it again and worked!
Conclusion: Looks that you need a virtualenv for this to work on Ubuntu 14 and python2. With python3 it works with and without virtualenv. I could not reproduce your error, but the idea is the same: it does not work because it looks for stuff in the wrong place.
Comments: There are two things you mention that I could not test:
I have been trying to get wagtail to work with my existing Django
application
With this big amount of heavy dependencies, (Pillow, Django rest, beatifullsoup and others I've never heard of) it would be strange if it worked out of the box with a previous setup. I mean, Django is a dependency for wagtail. So I would try the other way around. Start fresh with wagtail and port your code there (with python3 for the sake of the community ;) ). Also this manual copy of files is like programmers worst nightmare (at least to me). This looks like a version problem. Try to post the versions of everything involved, and the exact steps to reproduce the issue. I mean, did you pip install wagtail in the operating system that django was running?
I was having and uWSGI started.
So you have your app deployed already? This complicates things. If versions and virtualenv do not solve your problem, community needs setup details of deploy. Also you haven't mentioned your operational system.
I hope this helps! Good Luck!
I ran into the
cannot import name pages
error at:
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
while trying to deploy a wagtail app to Google App Engine and could not duplicate the problem on my local dev server.
A seemingly unrelated error that I saw only intermittently in GAE involved an import error in the PIL library. I was able to resolve BOTH errors by deleting PIL and pillow related folders from my vendored packages in my /lib directory. I had to import PIL through the app.yaml file instead due to the platform specific binary, but running pip install -t lib/ -r requirements-vendor.txt caused PIL to be installed into the lib folder as well (as a dependency of Wagtail)

Categories