Install Django Application so It's Available on Python Path - python

I have a django application structured like this...
app_foo
__init__.py
urls.py
views.py
models.py
bar_app
__init__.py
...
bar_app...
By using distutils, I can get the application to install into the python path under the "app_foo" module name.
However, any of the code inside of the "bar_app" python files which refers to things inside the django app relatively does not work when executed from the python path. For example,
from bar_app.views import stuff
I know that I can go through the app and change all the references to be absolute. For example,
from app_foo.bar_app.views import stuff
My question:
Is there anyway I can get all of the apps inside "app_foo" to also be on the python path?
Conceptually this would be similar to saying from app_foo import * for the entire path.

You can do
from .bar_app.views import stuff
http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports

Related

Django No Module Named With Inner Package Using

I want to write my own python package inside django's app. It looks like that: I have secondary app. It totally works except one thing. Someday I'd decided to make my app more structured. And there where problems started. First of all, I've added python package(with simple init.py inside directory) to the app. Then I've added second package inside that package. And when I try to run django.setup() inside packages' files, I'm getting this error: ModuleNotFoundError: No module named '<primary settings container module name goes here>'
How to make my custom package's functions working properly?
The problem is that settings module isn't "viewable" from your package, e.g. you should add the location of main directory to PATH variable, which could be done like this:
from sys import path
from pathlib import Path
path.append(str(Path(__file__).resolve().parent))
Or you can simply add it permanently to system PATH.

Django Cannot Import Local Module During Migrations

In my project i've got two custom objects, defined in a local fields.py file & blocks.py file. I import them in my models.py as such:
from . import fields as blockfields
from . import blocks
and when running migrations, the autogenerated migrations look like this:
import PROJECTNAME.fields
import blocks
and since blocks isnt in the local scope of the migrations folder, it throws an error. If i manually change it to "import PROJECTNAME.blocks as blocks" then it runs fine, but it'd be silly to have to do that every time. Any thoughts?
Updates:
Using Django 2.1.4, Python 3.6.7
Folder structure is thus:
PROJECTNAME
-fields.py
-models.py
-blocks/
--__init__.py
--*.py
-migrations/
--*.py
Ive variably tried having a blocks.py file which just points to the folder, but it doesn't change the import scope for the automigration.
In my blockfields, i refer to blocks also, which is how the references get baked into the migrations. The fields.py file has an extension of django's generic models.Field, while the blocks are custom types from scratch.

How to run script from parent directory in Flask?

I'm working on a Flask app and run into issues with trying to run a script within a module where the script is in a different directory. I've tried looking at several solutions here and on other sites and haven't been able to find something that works. I have a project structure like so:
dashboard\
app\
static\
templates\
__init__.py
jobs.py
api_fetch.py
config.py
run.py
Within jobs.py I have a function that needs to run api_fetch.py but for the life of me, I'm not sure what I need to do to that. I've tried imports with .., sys, os and nothings worked. This seems like it shouldn't be that difficult, but I'm at a loss. So far I've only needed to import modules on the same path which work fine.
in jobs.py:
from .. import api_fetch
then 'outside' dashboard:
$ python -m dashboard.app.jobs
__init__.py should be in dashboard also.
You can just import from parent package dashboard, the file structure should like this:
dashboard\
|app\
|static\
|templates\
-__init__.py
-jobs.py
-__init__.py << don't forget this!
-api_fetch.py
-config.py
-run.py
Then in jobs.py:
from dashboard import api_fench

Can a Python script use a Django database while not being in the same folder?

I have a python application (which we'll call app) I'd like to have a web front for. The application has many files and it's important the folder tree structure stays the way it is.
I've built a Django project and put it in a folder named "Web" in the app's folder, so now the folder tree looks like so:
[Data]
[Resources]
[Web]
[WebFront]
normal django app files
[Web]
__init__.py
settings.py
urls.py
wsgi.py
__init__.py
manage.py
main.py
Here's the code on the app's main.py:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.Web.settings")
django.setup()
This code causes an exception on the django.setup() line as (I think) django does not find the project modules: ImportError: No module named WebFront (WebFront is the name of the django app)
I suspect this is caused because django runs in the directory of python app, and therefore cannot find the folder WebFront - Which should actually be Web/WebFront
Can this be done? Or should I reverse the order and put the python app in the django app?
This is not a duplicate of the following questions as the folder nesting causes a different problem (I think)
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
Easiest way to write a Python program with access to Django database functionality
Using only the DB part of Django
You can locate your main.py script where you like. However, if it is outside of the Web folder, then you will have to add Web to the Python path, otherwise imports like import Webfront are going to fail.
import sys
sys.path.append('/path/to/Web/')
Once you have done that, you can change the DJANGO_SETTINGS_MODULE to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.settings")

Sys.path modification or more complex issue?

I have problems with importing correctly a module on appengine. My app generally uses django with app-engine-patch, but this part is task queues using only the webapp framework.
I need to import django settings for the app to work properly.
My script starts with:
import os
import sys
sys.path.append('common/')
# Force Django to reload its settings.
from django.conf import settings
settings._target = None
# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
I always get this error, or something related:
<type 'exceptions.ImportError'>: No module named ragendja.settings_pre
because the settings.py file starts with
from ragendja.settings_pre import *
I think I need to add ragendja to sys.path again but I had several tries that didn't work.
Here is my directory:
project/
app.yaml
setting.py
common/
appenginepatch/
ragendja/
setting_pre.py
myapp/
script.py
Is it only a sys.path problem and how do I need to modify it with the correct syntax?
Thanks
App engine patch manipulates sys.path internally. Background tasks bypass that code, so your path will not be ready for Django calls. You have two choices:
Fix the paths manually. The app engine documentation (see the sub-section called "Handling import path manipulation") suggests factoring the path manipulation code into a module that can be imported by your task script.
Eliminate dependencies on django code, if possible. If you can write your task to be pure python and/or google api calls, you're good to go. In your case, this might mean refactoring your settings code.
Why not:
sys.path.append('common/appenginepatch')
since the ragendja is in this directory?

Categories