Error on importing passenger_wsgi.py file - django

I'm currently trying to set up a Django project using passenger wsgi. I followed the instructions laid out on this post:
Update new Django and Python 2.7.* with virtualenv on Dreamhost (with passenger)
However, I'm receiving an error "An error occurred importing your passenger_wsgi.py"
I am able to successfully receive a hello word message if I put this as my passenger_wsgi.py:
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ["Hello, world!"]
But for some reason if I use the following (outlined in the above post), I am unable to get past the error. I replace the word 'project' with my named project on the path.append and os.environ lines and with subdomain.domain.com on the path.insert lines. Am I missing something? I am very new to this stuff and would appreciate any help I can get! Thanks.
Below is the current passenger_wsgi.py that receives the error on importing.
import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/project')
if sys.version < "2.7.3": os.execl("$HOME/<site>/env/bin/python",
"python2.7.3", *sys.argv)
sys.path.insert(0,'$HOME/<site>/env/bin')
sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
UPDATE I was able to get a passenger wsgi that imports but now I'm getting a 500 error. Here is what I'm sitting at right now:
import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), 'project'))
sys.path.insert(0, 'home/<site>/env/bin')
sys.path.insert(0, 'home/<site>/env/lib/python2.7/site- packages/django')
sys.path.insert(0, 'home/<site>/env/lib/python2.7/site-packages')
sys.path.insert(0, 'home/<site>/roommates')
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Once again I'm stuck though. I can't seem to find why I'm getting this error. The current errors that are showing up in the error logs are as follows per attempt:
Premature end of script headers:
Premature end of script headers: internal_error.html

You should change this line:
sys.path.append(os.path.join(os.getcwd(), 'project'))
into
sys.path.append(os.path.join(os.getcwd() + 'project'))
and make sure you change project with the name of your real project (or django app)

Related

ImportError: No module named sentry_sdk.integrations.wsgi

I'm trying to upgrade the implementation of the old Sentry config to the new one for my wsgi configuration in my django project but I'm getting the following error.
ImportError: No module named sentry_sdk.integrations.wsgi
Earlier I had:
from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
Now I have:
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
My sentry init is in settings.py file as:
sentry_sdk.init(
dsn=SENTRY_CONFIG.get("dsn", ""),
environment=SENTRY_CONFIG["environment"],
integrations=[
DjangoIntegration(),
CeleryIntegration(),
sentry_logging,
],
attach_stacktrace=True,
send_default_pii=True,
)
Using:
Django==4.0.3
sentry-sdk==1.5.8
python3.8
Any help is appreciated, thanks in advance!
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_environment = env('SENTRY_ENVIRONMENT',default='local')
sentry_sdk.init(
dsn=env('SENTRY_DSN'),
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
environment=sentry_environment,
)

python ConfigParser.NoSectionError: - not working on server

Python 2.7
Django 1.10
settings.ini file(located at "/opts/myproject/settings.ini"):
[settings]
DEBUG: True
SECRET_KEY: '5a88V*GuaQgAZa8W2XgvD%dDogQU9Gcc5juq%ax64kyqmzv2rG'
On my django settings file I have:
import os
from ConfigParser import RawConfigParser
config = RawConfigParser()
config.read('/opts/myproject/settings.ini')
SECRET_KEY = config.get('settings', 'SECRET_KEY')
DEBUG = config.get('settings', 'DEBUG')
The setup works fine locally, but when I deploy to my server I get the following error if I try run any django management commands:
ConfigParser.NoSectionError: No section: 'settings'
If I go into Python shell locally I type in the above imports and read the file I get back:
['/opts/myproject/settings.ini']
On server I get back:
[]
I have tried changing "confif.read()" to "config.readfp()" as suggested on here but it didn't work.
Any help or advice is appreciated.

PyCharm can't find 'SPARK_HOME' when imported from a different file

I've two files.
test.py
from pyspark import SparkContext
from pyspark import SparkConf
from pyspark import SQLContext
class Connection():
conf = SparkConf()
conf.setMaster("local")
conf.setAppName("Remote_Spark_Program - Leschi Plans")
conf.set('spark.executor.instances', 1)
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
print ('all done.')
con = Connection()
test_test.py
from test import Connection
sparkConnect = Connection()
when I run test.py the connection is made successfully but with test_test.py it gives
raise KeyError(key)
KeyError: 'SPARK_HOME'
KEY_ERROR arises if the SPARK_HOME is not found or invalid. So it's better to add it to your bashrc and check and reload in your code. So add this at the top of your test.py
import os
import sys
import pyspark
from pyspark import SparkContext, SparkConf, SQLContext
# Create a variable for our root path
SPARK_HOME = os.environ.get('SPARK_HOME',None)
# Add the PySpark/py4j to the Python Path
sys.path.insert(0, os.path.join(SPARK_HOME, "python", "lib"))
sys.path.insert(0, os.path.join(SPARK_HOME, "python"))
pyspark_submit_args = os.environ.get("PYSPARK_SUBMIT_ARGS", "")
if not "pyspark-shell" in pyspark_submit_args: pyspark_submit_args += " pyspark-shell"
os.environ["PYSPARK_SUBMIT_ARGS"] = pyspark_submit_args
Also add this at the end of your ~/.bashrc file
COMMAND: vim ~/.bashrc if you are using any Linux based OS
# needed for Apache Spark
export SPARK_HOME="/opt/spark"
export IPYTHON="1"
export PYSPARK_PYTHON="/usr/bin/python3"
export PYSPARK_DRIVER_PYTHON="ipython3"
export PYSPARK_DRIVER_PYTHON_OPTS="notebook"
export PYTHONPATH="$SPARK_HOME/python/:$PYTHONPATH"
export PYTHONPATH="$SPARK_HOME/python/lib/py4j-0.9-src.zip:$PYTHONPATH"
export PYSPARK_SUBMIT_ARGS="--master local[2] pyspark-shell"
export CLASSPATH="$CLASSPATH:/opt/spark/lib/spark-assembly-1.6.1-hadoop2.6.0.jar
Note:
In the above bashrc code, I have given my SPARK_HOME value as /opt/spark you can give the location where you keep your spark folder(the downloaded one from the website).
Also I'm using python3 you can change it to python in the bashrc if you are using python 2.+ versions
I was using Ipython, for easy testing during runtime, like load the data once and test your code many times. If you are using plain old text editor, let me know I will update the bashrc accordingly.

Apache is not interpreting python files

So, I followed this tutorial on Ubuntu 14.04
http://jee-appy.blogspot.in/2015/04/deploy-django-project-on-apache-using.html
Only thing I changed is instead of his example repository,I have used mine.
And I was getting error
Not Found The requested URL / was not found on this server.
Then, I edited my myproject.conf file
Change made - WSGIScriptAlias /myproject /var/www/myproject.wsgi
So, I'm having output Index of/ then directory and file name. it's not interpreting my python files.
Had the same issue and I modified my wsgi file to look like this:
import os
import sys
path = '/var/www/html/YOURAPP/'
if path not in sys.path:
sys.path.append(path)
path = '/var/www/html/YOURAPP/YOURAPP/'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Hope this helps

SublimeText2 and Django build

Just started using st2, switching from Eclipse pydev.
In Eclipse, I imported my Django project and could hit "Run" from any file and use the django framework.
for instance if I wanted to test something about my model class:
models.py
from django.contrib.auth import logout, login as auth_login
if __name__ == "__main__":
auth_login(username, password)
This would run and validate.
However, in sublime text, I can't get my python virtual_env to build the same way.
I keep getting the following error when I import anything django:
django.core.exceptions.ImproperlyConfigured: Requested settings CACHES, but settings are not configured.
One way to fix it is to ADD to EVERY FILE i want to test:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
I was wondering if there is an easier way to include this? My current build file is:
{
"working_dir": "/home/username/virtualenvs/workspace/myproject",
"env" : { "PYTHONPATH" : "/home/username/virtualenvs/workspace/myproject"},
"cmd" : "/home/username/virtualenvs/workspace/bin/python2.7",
"selector" : "source.python"
}
I've been searching around for hours...any help is appreciated!