Importing existing django project to PyCharm 3.4 professional - django

I have the following file tree sturcture
beta
├── data
├── libs
└── wsgi
├── openshift
└── static
which contains a django project. Django files are inside openshift under wsgi folder. I am trying to import it to Pycharm but It won't recognise the imports. I set content_root of my project the beta folder but still the same error. How can I successfully import an existing django project to a pycharm project?

Related

AWS lambda: "Unable to import module 'app': No module named 'models'"

I want to deploy a pytorch model with aws sam. my folder structure looks as follows:
├── Dockerfile
├── __init__.py
├── app.py
├── models
│   ├── common.py
│   ├── experimental.py
│   └── yolo.py
├── requirements.txt
└── utils
├── autoanchor.py
├── datasets.py
├── general.py
├── google_utils.py
├── metrics.py
├── plots.py
└── torch_utils.py
As you can see, I have several local dependencies. My Dockerfile looks as follows:
FROM public.ecr.aws/lambda/python:3.8
COPY app.py requirements.txt ./
ADD models utils ./
RUN python3.8 -m pip install -r requirements.txt -t .
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]
The code in app.py imports modules from models. Unfortunately this doesn't work and produces the following error:
"errorMessage": "Unable to import module 'app': No module named 'models'"
I have also tried to use COPY instead of ADD for the directories models and utils, but it results in the same error. How can I fix this?
Thing you are missing is if you trying load folder as package you have to create proper packaging in Python. Else you can get all files one by one imported in your app.py
Steps to create package :
Create folder name models
Create __init__.py file inside models folder
create your other files like yolo.py, experimental.py,common.py etc
Then load all four into __init__.py file like below
from common import *
from experimental import *
from yolo import *
Then you can use this in app.py like import models or from models import *
Note : Putting all files together into folder not work like package in Python.
Reference code : https://github.com/aviboy2006/flask-rest-api

Django i18n translations not working in production (Heroku)

My translations are working locally, but in production at Heroku, my site remains in its default language (English) after changing the language.
These are in my settings.py file:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
This is my structure:
myproject
├── Procfile
├── locale
│   └── fr
│   └── LC_MESSAGES
│   ├── django.mo
│   └── django.po
├── myproject
│   ├── __init__.py
│   └── settings.py
I thought it was a path issue so I SSH'd into my Heroku app and printed LOCALE_PATHS:
>>> from myproject.settings import LOCALE_PATHS
>>> print(LOCALE_PATHS)
['/app/locale']
And pwd in locale/ returns pwd
/app/locale.
What did I do wrong?
I found the issue:
my django.mo file was ignored by .gitignore as I use the default GitHub Python gitignore file.
The problem is that .mo files (compiled translations) are not present in the repo, and therefore not packaged to be deployed together with the rest of the application during the Heroku build process.
The possible solutions are to:
Add them to the repository
Generate .mo files during the build
I suggest to generate them during the build, for these reasons:
It automates compilation, one less manual step
It will ensure that the translations are always up to date
The compiled files are not source, and therefore should be in the repository
To generate them, you can use the post compile hook of the Heroku build:
Create a file bin/post_compile (no extension, like procfile) with the following line:
python ./manage.py compilemessages
Optionally you can add the specific language (e.g. python ./manage.py compilemessages -l nl)
I got this last part from this answer to a similar question.

Django and Collectstatic Issue

I'm trying to deploy my Django web Application (2.0.1) thanks to Nginx and I'm getting an issue.
I configured the new Ubuntu server, add my Django Project and I downloaded nginx.
My Django project looks like :
Mysite
├── App1
├── App2
├── App3
├── lib
├── Global_variables.py
├── Mysite
├── settings.py
I have to make collectstatic with nginx, so I execute this command :
python manage.py collectstatic
But into my settings.py file, I have :
#from django.conf import global_settings
import os, datetime
import lib.Global_variables
And this issue :
File "/var/www/Mysite/Mysite/settings.py", line 16, in <module>
import lib.Global_variables
ImportError: No module named lib.Global_variables
However my import seems to be right. Any idea ?
To make directory a python package you need to add inside this directory __init__.py file. From the docs:
The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path

Django: how to startapp inside an "apps" folder

I feel like I must be looking over something obvious because I am having trouble finding anything online about this. But basically here it is, I have a django Project, and I would like to store my apps inside of an apps folder, like thus...
myproject/
client-side/
media/
static/
templates/
apps/
app1/
app2/
etc.
__init__.py
manage.py
etc. (rest of root directory)
but have been unable to figure out how to >>>python manage.py startapp newapp and have it placed into the /apps/ folder. Is it ok to simply ok to startapp at the root level and manually move them into the /apps/ folder?
I greatly appreciate your thoughts and answers.
FYI running python 3.4.2 and Django 1.9.5 on Windows 10
You can specify the app's directory as a second parameter:
python manage.py startapp <app_name> <app_directory>
Note that no directory will be created, the app's files will be created directly in the specified directory. Example:
python manage.py startapp myapp apps/myapp
Will result in the given directory structure:
apps
└── myapp
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py
Also note that the command won't create the directory for you.
Edit: as another (now deleted) answer pointed out, running the command from the apps directory would also work:
cd apps
python ../manage.py startapp myapp
You can also use the django-admin command as well.
cd apps && django-admin startapp app_1
this will work as well

Understanding directory structure advice

I started to create in Django sample project, first command:
django-admin.py startproject test
gives me:
- root
- test
- __init__.py
- settings.py
- urls.py
- wsgi.py
- manage.py
Now I create first app:
python manage.py startapp foo
it created for me folder root/foo
so how I should understand my root/test folder. Is this folder for global config of my project and nothing more? (similar to Symfony 2 app folder)
I am confused because Django docs tells:
The inner mysite/ directory is the actual Python package for your
project
but manage.py startapp foo create app under root, not under root/test (mysite equivalent)
[EDIT]
Two commands:
python manage.py startapp app
and:
django-admin.py startapp app
gives me app inside project root, not under root/name_of_generated_project
Django 1.4
[EDIT] 2
Sorry guys, my fault, now is everything ok.
[EDIT] 3
I want to create another project again:
django-admin.py startproject jobeet
my initial structure is similar to above.
Now I want to try create app (inside jobeet folder):
django-admin.py startapp jobs
and I end up with jobeet/jobs not jobeet/jobeet/jobs
again :/
so inside my project root I have:
- jobeet
- jobs
- manage.py
another command:
python manage.py startapp app
gives me the same result
So let's say you create a new Django project testproject:
django-admin.py startproject testproject
This creates a new project with the following minimal set of files:
testproject/
├── __init__.py
├── manage.py
├── settings.py
└── urls.py
To create a new app for your first site mysite1 go into testproject directory and run:
python manage.py startapp mysite1
which results in a new directory mysite1 for the mysite1 app.
I.e. with just these two commands you would arrive at this hierarchy:
testproject/
├── __init__.py
├── manage.py
├── mysite1
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── settings.py
└── urls.py
Refer to the django-admin.py and/or manage.py individual commands here.
In Django there is a one-to-many relationship between a project and an app. An app is usually one individual site component (e.g. comments, ratings), whereas a project is an organisation of several apps and can power many different sites. That's why you get the sites framework. In practice, one project usually serves one website, but with Django sites app with one project you can serve as many websites as you like, for reusability's sake.
P.S. I think creating a project simply called test is not a good practice because with Django unit tests at app level unit tests will go into a file called tests.py or within a folder called tests.
UPDATE for Django 1.4
As #zeantsoi has commented below, my answer:
applies to Django 1.3 and prior. Per the docs, beginning in 1.4, base
configuration files (including settings.py, urls.py, and wsgi.py) are
abstracted into a subdirectory titled the same name as the project.
"test" is the top level of your project. I've never used symphony 2 so I can't comment on that, but it seems like you have a grasp on it. The files that live in there are basically all global config files. Inside your "test" folder you should also have one or more app folders. Inside these app folders live the apps specific models, views, urls, etc.
It seems you've got something a little wrong your foo app should live in root/test/foo not root/foo.
Now in my projects I tend to have things like a virtualenv folder live in the root dir, but you definitively shouldn't have apps at that level (it just won't work)
manage.py doesn't provide a startproject command - that's usually a django-admin command. I'd check which manage.py you're executing, and ideally, use the manage.py from the project directory you've created.