I am trying to access my Django models from a script that will be run outside of any Django views. It will be a standalone script and not related to any Django views etc, however, I would like to be able to use the Django ORM to update fields in the database.
My standalone script is in the data directory:
I have tried endless suggestions from SO, such as the code below, but I keep hitting a similar issue - "ModuleNotFoundError: No module named 'nft.settings'"
import django
import sys
import os
sys.path.append('/Users/chris/Documents/Python/Django/nft/') # This path being the directory at the top of my screenshot
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nft.settings")
django.setup()
from nft_app.models import Object
Any help would be greatly appreciated
CodePudding user response:
The short answer is that python looks for packages in the current directory and the PATH. Since you run python script.py
in the nft/data
directory, it looks for nft/data/nft
for the nft
package.
One solution to this is to add nft/nft
directory to the PYTHONPATH
environment variable.