Django not finding apps in virtualenv when using manage.py syncdb - django

My problem is in getting manage.py syncdb to run within a virtualenv.
It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.
Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.
When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.

Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,
For development, to create self-contained environments
For deployment, to create self-contained environments
In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.
Modified manage.py
#!/usr/bin/env python
import os.path
# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:
TopLevelDir
|
|- Project DIR
|- venv
|- requirements
|- deployment configs

I have a simple solution to this
Just launch manage.py from the python in the bin of your virtual environment.
So say your python is here /home/tom/environments/my_env/bin/python you could launch manage.py like so:
/home/tom/environments/my_env/bin/python manage.py syncdb
then just create a symlink to the virtual environment's python inside your django project and call it env_python then you can do this:
./env_python manage.py syncdb

Related

Celery in production raise ModuleNotFoundError: No module named 'celery'

In developement all is good, app starts without any problems. However, in production, Gunicorn can't start due ModuleNotFoundError: No module named 'celery'
Celery is installed properly
>>> from celery import Celery
>>> print(Celery)
<class 'celery.app.base.Celery'>
already tried changing file name to anything other than celery.py - did not help
Any thoughts?
Here are a few things you can try to resolve the issue:
Check if the Python version used in production matches the one used in development. If it's different, make sure that Celery is installed for the correct version of Python.
Check if the virtual environment is activated when running the app in production. The virtual environment should contain all the required packages, including Celery.
Check if the Celery module is installed in the correct location. The module should be located in the site-packages directory of the Python environment.
Try to run the command pip show celery in the environment where you are running the app in production. This command will show you the location of the Celery package and the version number. Make sure that it matches the version installed in your development environment.
Try to run your app with the following command: python -c "import celery". This will check if the Celery module can be imported without any issues.

django project not executing makemigrations command as expected in vscode editör

I have a django project. I moved my project from pyCharm editor to vscode editor. When I run the python manage.py makemigrations command, it creates a migration file for some applications under venv. I think it depends on the LANGUAGE_CODE value in settings.py but I used gettext_lazy in all my models. Even if I type the command python manage.py makemigrations app_label, it still creates migration files for projects under venv. The libraries installed with pip install under venv are my own django projects. I have always used gettext_lazy in these projects. Same projects execute makemigrations in pyCharm editor with no issues

Django Rest Framework — no module named rest_framework. Rest_framework is installed and venv is active

I clone repository from github on my second computer, runserver does works but when typing
python manage.py makemigrations
Bash notice about 'no module name rest_framework'
This situation is a second time. Lately I install again rest_framework but then I had install other modules. Finally I create new project but now I want resolve this problem.

manage.py runserver - ImportError: No module named MYSITE

I can't get the dev environment running!
I've been following the instructions here (tutorial on DjangoProject page). When I type
manage.py runserver
I get the following error:
ImportError: No module named MYSITE
I am executing the command within the folder MYSITE which has the files init.py, manage.py, settings.py, urls.py
I've searched around and found questions on "manage.py runserver", but not this specific error. Your help is greatly appreciated. Thanks
If you're using windows, you'll need to put your project's directory (the one with manage.py in it) into an environment variable called PYTHONPATH.
Since this is highly ranked on Google and I got here while searching for an answer I'll share my solution:
I have a project built on my machine, packaged into a debian package and installed on a test machine. On my machine I use the runserver but on the testmachine I use apache (which was using fine). When changing database from sqlite to postgresql I could not do shell or syncdb etc (all manage commands). The problem here was that manage.py was owned by www-data and it had to be root for these commands to work...
sudo chown root. manage.py
Another different answer ;) I'm walking the "Django for the Impatient: Building a Blog" chapter from "Python Web Development with Django" and it suggests creating a "dotted" module so I registered my application as "x.y" which it didn't like. Changing to "y" which matched the file-system as:
/x
+ manage.py
+ /x
+ settings.py
+ /y
+ models.py
Worked for me.

Running manage.py dumpdata on apps with dots in their names

I'm trying to do moving some data from my development machine to a server using dumpdata but ran into a problem.
So, say I want to dump the data that belongs to the app django.contrib.auth.
django.contrib.auth is in my INSTALLED_APPS.
This happens when I run
$ python manage.py dumpdata django.contrib.auth
Error: Unknown application: django.contrib.auth
The strange thing is that I can do manage.py testserver (i.e. nothing is broken) or do
$ python
>>> import django.contrib.auth
So there is nothing wrong with the python path.
I can run dumpdata on apps that are located straight in my project's dir.
If I leave out the apps' names, django.contrib.auth's tables are dumped as expected.
So, why can't I point out a specific app with dots in the name? I have tried to dump
other apps that are located in site-packages with the same result.
Try instead:
python manage.py dumpdata auth
The dumpdata command doesn't require the (fully qualified) package name of the app, only the name.