Django app conflicts with an existing Python module - django

I have a function that generates a bash script file '.sh' to create a django project and a few apps.
This is the bash file I generate:
#! /bin/bash
cd projectx
source projectxenv/Scripts/activate
python manage.py startapp app1
python manage.py startapp app2
deactivate
When I execute the bash I generated, it gives me the following errors:
django.core.management.base.CommandError: 'app1' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
django.core.management.base.CommandError: 'app2' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I don't think app1 and app2 are existing modules, and also, I tried running these commands manually and they work, but when I try ./bash.sh, it doesn't work.

Related

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.

Django, can't create app in subfolder

I'm on windows and trying to create a new app inside a common folder named Apps.
The myApp folder already exists inside Apps. I'm running from the project root:
python manage.py startapp myApp Apps\myApp
and I get:
Error: 'Apps\\myApp' is not a valid app name. Please use only numbers, letters and underscores.
I don't know why that double backslash.
I tried also with a forward slash just to be sure:
python manage.py startapp myApp Apps/myApp
and I get this:
Error: 'myApp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I can't understand if it is a Windows or a Python problem.
Try this:
mkdir Apps\newapp
python manage.py startapp NewApp Apps/newapp
And you will to create a app called "NewApp" inside folder "Apps/newapp".
Create your Apps directory from your project's root directory-
mkdir Apps
Move to your Apps directory-
cd Apps
Run python by calling the manage.py in your root project directory-
python ../manage.py startapp newapp
There you go
from the docs:
If the optional destination is provided, Django will use that existing directory rather than creating a new one. You can use '.' to denote the current working directory.
django-admin.py startapp myapp /Users/jezdez/Code/myapp
So try python manage.py startapp myApp ./Apps/myApp or with the full path.
Manage.py file is a thin wrapper of django-admin.py
In case, you want to create a new app in any directory
Try this:
$ cd <directory path>
$ django-admin.py startapp <app-anme>
For django 3.2.9
Create a sub directory Apps to hold all the apps, move into it, create a directory for the app myApp, then come back to root directory
mkdir Apps && cd Apps && mkdir myApp && cd ..
add a __init__.py file (is a python way to treat a directory as a package)
Create a myApp inside Apps sub directory
manage.py startapp myApp Apps/myApp
(in windows, please use backslash)
above two lines of code will create myApp
Now, in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local
'Apps.myApp.apps.MyAppConfig', # new
]
in ./Apps/myApp/apps.py file, change name='myApp' to name='Apps.myApp' will solve the issue
I had the same trouble on my Mac as well. I did solve it upgrading Django from vervion 1.3 to version 1.4.
As the documentation says you can use the command
django-admin startapp name [directory]
but in the example
django-admin startapp myapp /Users/jezdez/myapp
the documentation does not say that python creates the myapp folder. You should do it before the startapp command.

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.

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

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