django-admin.py and python path on EC2 Amazon Beanstalk - python

I deployed my django app on Elastic Benastalk, but my commands are failing and I think that the problem is that django-admin.py is not in the $PYTHONPATH, I would like to add my app to the python path, but I don't know what is the exact path on the EC2 instance.
Actually I found it under: /opt/python/bundle/3/app (I used "find" command via SSH)… but is that a fixed and reliable path??
ps: WTF is that "3"?? (for sure not the version or the count of my deploys ^_^)
UPDATE:
if I cd to /opt/python/bundle/3/app/myappname and run:
python manage.py shell
I get:
File "manage.py", line 3, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
SOLVED!
Amazon Beanstalk uses a virtualenv (like I do myself locally), in order to activate it you have to:
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
python manage.py commandname
BUT, in order to use custom management commands (that is why I need to access to the django shell on my EC2 instance), you have to add your application to the python path and also all the environment variables used by your app, so I did:
vi /home/ec2-user/.bash_profile
and added:
export PYTHONPATH=$PYTHONPATH:/opt/python/current/app
and my env variables... now it works! :)
In order to automatically activate the virtualenv and to be able to use the django shell as soon as logged via ssh, is it possible to add:
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
in the .bash_profile :)

You can add an option_name if you need to change PYTHONPATH, or set any environment variable in general.
In your .ebextensions/myapp-env.config file (or whatever your *.config is named):
option_settings:
- option_name: PYTHONPATH
value: /opt/python/ondeck/app/myapp:$PYTHONPATH

The message:
File "manage.py", line 3, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
is not because your app is not in the PYTHONPATH but rather because it can't find your django application at all. Meaning your site-packages directory is not in the PYTHONPATH.
Try to find the site-packages directory in your server and that should in the PYTHONPATH. I haven't deployed a python app with Elastic Beanstalk but I believe it maybe using a python virtual environment. In which case you need to source your virtual environment before running python ./manage shell

Related

Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? --> WSGI.PY file

When I try to execute py manage.py runserver I had the following message:
raise ImportError(
Import Error: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
)
I found a lot of possible solutions on the internet. I suggest first try the following:
Go to the wsgi.py file, inside MySite and go to this line:
from django.core.wsgi import get_wsgi_application
Verify that Django is imported. I use Pycharm, the IDE allowed me to import directly from there.
After this I was able to run manage.py
Try to make it in a virtual environment, it will work:
First, in cmd type pip install virtualenvwrapper
Make a folder in c drive which will consist all of your django projects like C:/Django
Then go to cmd and type mkvirtualenv [name you want to give] and then close the cmd
Open it again and then navigate to the folder C:/Django then when on that folder type workon [the name which you gave]
Finally, install django by the pip command and then runserver

Cookiecutter created directory giving me issues running development server and python shell

I created a django project using cookiecutter as reccomended by Two scoops of Django 1.8. It's called icecreamratings_project
I use the git cmd prompt and use
'cd icecreamratings_project'.
When i want to use the built-in python interpreter by using
python manage.py shell it gives me the following error.
File "C:\Users\Armando\Desktop\icecreamratings_project\config\settings\common.py", line 13, in
import environ
ImportError: No module named 'environ'
I looked into the directory and the following code is there:
from __future__ import absolute_import, unicode_literals
from sys import path
import environ
ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path('twoscoops_project')
env = environ.Env()
No module named environ exists, but I'm assuming environ is in reference to the virtual environment. Im not familiar with the cookiecutter documentation or how it creates django templates, but i created a virtual environment named environ.
The message i got after that is that there is no Path in environ. Can someone help?
The environ module can be found in django-environ.
django-environ is a requirement of cookiecutter-django's requirements/base.txt.
base.txt is a requirement of cookiecutter-django's requirements/local.txt.
It seems you'll install environ and other needed modules by completing the following steps from cookiecutter-django's README.rst:
Getting up and running
The steps below will get you up and running with a local development
environment. We assume you have the following installed:
pip
virtualenv
PostgreSQL
First make sure to create and activate a virtualenv, then open a
terminal at the project root and install the requirements for local
development:
$ pip install -r requirements/local.txt
Source: https://github.com/pydanny/cookiecutter-django#getting-up-and-running

how to correctly set environment setting for more than one projects in Django?

I have a project called "myplanet" and my manage.py‍ file looks like:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myplanet.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
I know that I have to set a system variable DJANGO_SETTINGS_MODULE:myplanet.settings and also set the PYTHONPATH to my settings.py file. I was wondering what I should do in the case of another project that is called gMaps ? I tried to do the same but it does not simply work. My OS is Windows 7 x64
You can set up two independent virtual environments for each project. It's best practice I think. In that case you can even install different versions of python packages for each project as it required.
You can read about using virtualenv and it's wrapper here:
http://virtualenvwrapper.readthedocs.org/en/latest/index.html
You don't need to set PYTHONPATH variable environment to your django project for running that, when your settings.py is inside a package or is a module that is importable with manage.py.
The PYTHONPATH is a list of directories Python goes through to search for modules and files.
If you need to add a path of one library or your root path of project to it, you can do that in your code.
For example in manage.py with:
import sys
sys.path.append("/home/my/project")
Or:
import os
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(BASE_PATH + '/src/folder/of/my/project/')
And each django project has own manage.py‍ file that set DJANGO_SETTINGS_MODULE enivoronment itself, and you don't need to set that.
I think if your manage.py have this line:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myplanet.settings")
Then, you just need to have this structure for your project:
myplanet # It's a project folder
myplanet # It's a package
__init__.py
settings.py
manage.py
And you can run your project with python manage.py runserver without set PYTHONPATH environment variable.

How does django manage.py put a project package on sys.path

I read the django doc and some SO posts to know the differences between manage.py and django-admin.py.
They all say:
manage.py is automatically created in each Django project. manage.py
is a thin wrapper around django-admin.py that takes care of two things
for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to
your project’s settings.py file.
So I checked the scource code of these 2 files(latest version, so it the doc).
Then I am confused. manage.py does the second thing: sets the DJANGO_SETTINGS_MODULE environment variable. Besides that, I really can not find any differences between these 2 scripts.
[django-admin.py]
#!/usr/bin/env python
from django.core import management
if __name__ == "__main__":
management.execute_from_command_line()
[manage.py]
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Why? Is the django documentation out of date? Or I missed something here? And where is the code that puts the project’s package on sys.path?
The sys.path is updated here using handle_default_options(options) statement located here. The execution path is as follows:
execute_from_command_line(argv) (your manage.py)
utility.execute() here
handle_default_options(options) here
The same method is used by Command class used as base class for management commands.
From what I can see it's not ./manage.py who "puts your project’s package on sys.path."
The doc says:
sys.path is initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default.
The installation-dependent default must be what site.py adds. But it can be disabled with -S switch. Now then when I run this script (1.py):
import sys
print(sys.path)
I get:
$ python -S ../1.py # to show that it's not current directory that is added
['/path/to/the/script/directory', '/usr/lib/python35.zip', '/usr/lib/python3.5/', \
'/usr/lib/python3.5/plat-linux', '/usr/lib/python3.5/lib-dynload']
So, when you run django-admin, /path/to/env/bin/django-admin will be the first on the sys.path. And when ./manage.py, /path/to/project.
As such, one can probably say that ./manage.py "puts your project’s package on sys.path," but indirectly, by being at the root of your project.

Import Configurator Error for Python Pyramid

i'm trying to learn python pyramid for linux and following the pylonsproject documentation in doing so.I'm also new to linux.
I've installed everything correctly(I'm pretty sure), but when i invoke my helloworld.py, i got this following error
ImportError: No module named pyramid.config
the documentation says this should be the path to the file
$ /path/to/your/virtualenv/bin/python helloworld.py
but i'm confused as the python after the bin is a executable not a directory? my env is located in the Downloads folder
Thanks!
These symptoms are almost always a misuse of the virtualenv in one way or another.
1) Make sure you are running the virtualenv
$ env/bin/python helloworld.py
2) Make sure you installed pyramid into the virtualenv
$ env/bin/python
>>> import pyramid.config
# ImportError or not?

Categories