Django, can't create app in subfolder - django

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.

Related

Django app conflicts with an existing Python module

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.

No module named 'mysite.settings'

I know this question has been asked quite a lot but all the solutions given do not seem to fit for me.
I run this in a venv with python 3.6.8 and django 2.2.10
When I run django from cli it works and all functionality is working great so I know it is not django itself that is failing me.
Path to wsgi script is "/opt/sites/aws/okta/wsgi.py"
Actual wsgi.py:
import os, sys
sys.path.append('/opt/sites/aws')
#path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sys.path.append(path)
print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "okta.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
When running python to see what errors it gives I get
ModuleNotFoundError: No module named 'okta.settings'
I get the same error when running apache with debug logging.
I have quadruple checked the path and the environment variables and they are set right in the wsgi.py script.
Asked for the INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'okta_oauth2.apps.OktaOauth2Config',
'ops.apps.OpsConfig'
]
aws/
bin/
lib/
lib64/
logs/
okta/
__init__.py
urls.py
settings.py
wsgi.py
okta_oauth2/
ops/
share/
static/
manage.py
pyvenv.cfg
Although I did not figure out what was wrong. This is what I did to resolve.
Note for anyone reading this in the future, please DO NOT do this first. I tried other more common results like ImportError: No module named mysite.settings (Django) to fix the issue first and when none worked I tried this.
cd /opt/sites #Just outside the venv
sudo service httpd stop # stop web server
mv aws aws_broken_20201223 # move your broken env to a new name but do not delete
python3 -m venv aws #create new venv
cp -R aws_broken_20201223/(django dirs) aws/ #copy all the custom apps and project you have to the new folder
cd aws #go to the new install
bin/activate #activate your env
pip 3 install -r requirements.txt # I had a predefined requirements.txt for all mods I needed. I would suggest you do the same before doing this.
python3 okta/wsgi.py # Test to see if you are still getting the not found error
sudo service httpd start # start web server back up
voila it works without issue, no config change at all. Very strange but I'll take a working production site then a broken one.

Structure in Django tutorial does not match match my strucuture

Starting a new project and realized that the structure for the tutorial and my structure do not match. It looks like a directory was created for the myproject and a directory AND a file were created for myapp... I don't understand. Is this correct?
Tutorial structure shows:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
My structure shows:
mysite/
myapp
myappp
__init__.py
settings.py
urls.py
wsgi.py
manage.py
#NinaPy, Your directory structure seems to be correct but there exist an issue.
Please follow the steps:
1) Go to your directory where you want to keep your project. Example--myproject folder
cd myproject
2) Now you are within your desired folder. run the command from CLI(Command Line Interface)
django-admin startproject mysite //mysite is the project name
it will create
1 directory[mysite] and 1 file[manage.py]
3) Now if you want to create an app: Go to mysite directory because manage.py exist in there.
cd mysite
run the command from CLI
python manage.py startapp myapp
It will create myapp in same level.
myproject/
mysite
manage.py
myapp
If you use the CLI then you are quite safe for folder directory and file structure. Do not try it to do manually unless you need some extra files.
Please have a look on here:
https://docs.djangoproject.com/en/3.0/intro/tutorial01/#creating-a-project
your can run python manage.py startproject to create a new project
yes its correct
before run type on terminal(enter the directory mysite)
cd mysite
then run
python manage.py runserver
python manage.py startapp appmane
........
..........

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.

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