IIS can not access newly added files in a django project - python

In a django project that is served by IIS (windows), i added a local file, test.py. The project ran perfectly before and still runs perfect on localhost, however IIS appears to not recognize the new test.py file. It appears IIS access to this file fails, even if the users IUSR and IIS_USRS have full access (same as for all other files in the folder).
I get below error message, and somethimes also the same but "No module named 'app.test'. Removing the "import app.test as test" in views.py solves the issue.
Suprisingly the "import app.oldFile as oldFile" works without issue.
In my views.py, i import the python scripts like this
import app.oldFile as oldFile
import app.test as test
My django project has the structure:
djangoRest
-app
--__init__.py
--views.py
--oldFile.py
--test.py
-djangoRest
--__init__.py
--settings.py
--urls.py
--wsgi.py

I found the reason, and solution.
In the earlier import file oldFile.py i use the line
import os
os.chdir(r\\some\\other\\directory\\)
This obviously changes the current working directory and as a result the other file test.py can't be found, and cannot be imported.
This mistake does not matter when testing django on the localhost, but only manifests in an issue when served by IIS.

Related

How to tell passenger_wsgi.py to look for Django project inside another folder?

I'm trying to host a django app on cpanel but i cant find a way to tell passenger_wsgi.py to look for the django project(main file) inside another folder
My site structure is:
/home/project/
tmp/
public/
passenger_wsgi.py
abdi-group/
passenger_wsgi.py:
from abdiGroup.wsgi import application
this works fine if i move everything inside abdi-group/ to /home/project/
I tried this:
passenger_wsgi.py:
from abdi-group.abdiGroup.wsgi import application
but it can't find abdiGroup(django project name) inside abdi-group/
am i missing something?
abdiGroup is supposed to be a folder in your project directory that contains the wsgi file not abdi-group. If you're making it a subfolder add an empty __init__.py to any new folder so Django knows it is a package and can import from it.

Django, importing models class into new file. Import error attempted relative import with no known parent

I am working on learning Django by making a simple analysis dashboard. I did the startproject command and the startapp command like the tutorial ran through. I added a new file called connectionOracle.py in the app folder.
My folder structure is (top folder is was created via venv)
AnalysisSite
|AnalysisSite
|coreAnalysis
||models.py
||connectionOracle.py
I have a class called WorkOrder in the models.py file. I am trying to import it into the connectionOracle.py file by doing the following
from .models import WorkOrder
I then go to the Command Line (on windows) and navigate tot he coreAnalysis folder and run the following
python connectionOracle.py
I get an error that says.
ImportError: attempted relative import with no known parent package
I did some reading online, and I tried doing an absolute path with AnalysisSite.AnalysisSite.coreAnalysis.models
that didnt work. I also tried moving the connection file to different directories and that didnt work either. I also tried going into the command line and typing set DJANGO_SETTINGS_MODULE = AnalysisSite.settings
I also put a _init_.py in each folder. (Django automatically put it into the project directory and app directory).
I am not sure what I am doing wrong
you are trying to access Django components(models) by a script file ,
the caller in this case not Django itself ( the request not coming from url or different django tools or mechanism),
anyway in your custom python file which is 'connectionOracle.py'
try to do some steps before accessing the models itself,
the steps are available on the following URL:
https://stackoverflow.com/a/68936419/12662056
############
change the path for project depending on your project path.
i hope this helpful

Chainging directory of flask project - everything fails

Hi I had followed project organisation in directory ./my_project/
-venv/
-static/
-templates/
--index.html
--login.html
-requirements.txt
-main.py
-views.py
then i wanted to add test folder and reorganise all to following structure:
-venv/
-my_project/
--static/
--templates/
---index.html
---login.html
--requirements.txt
--main.py
--views.py
And when I am running main file, server runs, but if I open on a browser right address there is error 404. In log of a server I see that server recieves request, but something is wrong. I have no idea what is this, because I didn't change anything in code.
You need to make the my_project a Python package by adding an empty __init__.py file inside it.
See this answer here https://stackoverflow.com/a/448279/9379795

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")

Getting ImportError: no module named wsgi after changing settings

I have a django app deployed on Heroku.
I was using a single settings file which I had to change (mostly, changing the database) each time I had to run it locally or deploy it on Heroku.
I decided to change that and created a settings module (a settings folder with an __init__.py file) with different settings files (development.py/production.py) for different environment. I also changed the manage.py file to get the development settings file like this:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.development")
And change wsgi.py to grab the production settings file:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.development")
After I did that, the local development server is now running fine.
But when I deployed the code on Heroku, I get
"ImportError: no module name wsgi"
error in heroku logs.
I have tried several things for the last few hours but all in vain.
Can somebody guide me what mistake I am making?
Thanks in advance.
Note: 'project' is the name of my project.
After 2 horrible struggling days, I figured that the culprit was init.py that I accidentally put into my project's main folder and Heroku was treating it as a module. Removing init.py solved the problem.

Categories