ImportError using python if __name__ == "__main__" - django

I currently inherited a codebase that looks something like this.
project
manage.py
|_ config
|_ settings
|_ wsgi.py
|_ project
|_ app1
|_ app2
|.... <-- many more Django apps
|_ a_new_app
|_ __init__.py
|_ run.py
|_ foo.py
|_ bar.py
I added a new app with some .py files which imports from other apps too in the same package and other app packages too in the project. All is well till I tried to run
python project/a_new_app/run.py
Then I started getting import error here is how my run.py looks.
# run.py
from project.a_new_app.foo import Foo
class App():
def method(self, key):
data = {"some-key": Foo}
return data.get(key)
.... more methods here
if __name__ == "__main__":
app = App()
app.loop_forever()
I got this error
File "project/a_new_app/run.py", line 7, in <module>
from project.a_new_app.foo import Foo
ImportError: No module named project.a_new_app.foo
My working directory is /user/me/PycharmProjects/project, Thanks.

from project.a_new_app.foo import Foo
For this import to work, you need the outer project directory (the one containing manage.py and the inner project directory) to be on the Python path.
However, run.py is two directories deeper than that, in project/a_new_app. Therefore you need to add ../.. to the python path at the top of the module.
import sys
sys.path.append('../..')
from project.a_new_app.foo import Foo
...

Related

How to use startproject --template? (Django 2.2)

I'm attempting to create a Django templates directory via django-admin startproject and --template.
I've tried: django-admin startproject main_project --template=./templates
However it only creates an empty main_project directory there is nothing reflecting a Django instance in this directory (ie. no urls.py, settings.py, wsgi.py, etc.)
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ templates/
I'm expecting a project Python package to be created as described here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/#creating-a-project
mysite/
manage.py
mysite/ # or main_project for my example
__init__.py
settings.py
urls.py
wsgi.py
I'm only getting:
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ main_project
|_ #empty directory
|_ templates/
|_ venv
As per the docs https://docs.djangoproject.com/en/2.2/ref/django-admin/#cmdoption-startapp-template
Perhaps try the command with the —template flag before the project name

Django + Heroku celery module not found

I'm trying to push my lastest version of my webapp live to Heroku. I'm pretty comfortable pushing to Heroku and this just doesn't seem right. For some reason I feel like Heroku is skipping my requirements.txt. Just in case I manually installed celery on my Heroku app.
I'm getting this specific problem with celery, but if Heroku is skipping my requirements.txt this might be a bigger problem.
1. If I run:
heroku run pip install celery
This let's me install the package over and over, shouldn't it kick back a "requirement already met" error?
2. When I try to push to heroku, I keep getting a
File "/app/config/_celery.py", line 4, in <module>
from celery import Celery
ModuleNotFoundError: No module named 'celery'
for the life of me I can't figure out why, I've uninstalled celery, reinstalled it locally. It's on my requirements.txt (Heroku should install it upon push to the remote). celery also seems to work locally just fine.
I'll include what I think is necessary, but let me know if I'm missing something that might provide the answer.
Here's my projects file structure:
POTRTMS(overall project folder)
|
+-config(holds settings)
| |
| +--settings
| | |
| | __init__.py
| | production.py
| | local.py
| | base.py
| |
| _celery.py
| __init__.py (this is the __init__.py i'm referencing below)
_celery.py
from __future__ import absolute_import, unicode_literals
import sys
import os
from celery import Celery
from celery.schedules import crontab
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production')
app = Celery('POTRTMS')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
app.conf.beat_schedule = {
'insurance_date': {
'task': 'insurance_date',
'schedule': crontab(minute=0, hour=8),
},
}
__init.py__
from __future__ import absolute_import
from ._celery import app as celery_app
I was able to figure out the problem. Inside of my project root folder I had Pipfile and Pipfile.lock which sounds like a new way to do the requirements.txt.
I'm not sure how these files got into my project but after deleting them all is working.

Django: how to set the path for the environ variable "DJANGO_SETTINGS_MODULE"

In Django, I used to write populating scripts and put them in the project root directory. For example,
mysite/
mysite/
manage.py
populateA.py
The first few lines of populateA.py:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
...
As there are more and more populating scripts, I would like to move them to another package populate:
mysite/
mysite/
manage.py
populate/
__init__.py
populateA.py
populateB.py
populateC.py
...
However, when I run the populating scripts (python populateA.py), I got the error message: ImportError: No module named 'mysite'. How to properly set the path for DJANGO_SETTINGS_MODULE?
Since populate is already a module, run its submodules.
python -m populate.populateA

Importing file from another folder in python

Im using Python 2.7.
I got structure in PyCharm (all dirs are packages):
dirExa\
dirBar\
Bar.py
__init__.py
dirFoo\
Foo.py
__init__.py
__init__.py
Now i want to import module called Bar.py in Foo.py
I used this (in file Foo.py):
from dirExa.dirBar import (some method)
but it gives me error such: No module dirFoo.dirBar import (some method)

Scrapy project can't find django.core.management

I'm trying to follow the method here to 'Scrapy' data from the web and simultaneously save that data directly to my Django database using Scrapy's item pipeline.
However, when I try to run scrapy crawl spidername, I'm getting the error:
ImportError: No module named django.core.management
At first I thought it was because my Scrapy project was outside of my Django project folder, but even after I moved the whole project into my Django project folder I kept getting the same error. If I open a python shell inside the Scrapy project folder in its new location (inside my Django project folder), import django.core.management works fine. So what's going on?
EDIT: Some additional info: I'm doing this on a Webfaction server, and the path to my Django project is /home/gchorn/webapps/django_app/django_project. I'm using Django version 1.4.1, Scrapy 0.16 and Python2.7. The layout of the Django+Scrapy project is as follows:
django_project/
__init__.py
manage.py
settings.py
urls.py
myproject #folder containing wsgi.py
app1
app2
app3
templates
ScrapyProject/
scrapy.cfg
ScrapyProject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
my_spider.py
Try setting this in your Spider's settings.py:
import os
import sys
sys.path.append('/home/gchorn/webapps/django_app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Then you can import your model classes like:
from django_project.app1.models import some_model