Sphinx ignoring imports (py2.7) - python-2.7

Sphinx seems to be ignoring imports in autodoc'ed modules.
doc/conf.py excerpt
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
sys.setrecursionlimit(1500)
doc/code.rst
Auto Generated Documentation
============================
Utils
-----
.. automodule:: myproject.utils
:members:
myproject/utils/__init__.py
from data import *
from hashing import *
from redis import *
from cookie import *
from route import *
def method_whose_docstring_is_picked_up(string):
"""I'm being autodoc'ed. =)
"""
None of the classes or functions from X import Y are being autodoc'ed. Anything directly in the __init__.py is being picked up just not the imports. I tried explicitly importing each object but that didn't resolve anything.

Turns out this is possible with the imported-members but it will also drag in all std-lib and third-party imports as well, cluttering up your sphinx doc.
Sphinx, using automodule to find submodules

Related

The `__init__.py` import multiple modules while they are not utilized within the file

I am reading /django/forms/__init__.py
"""
Django validation and HTML form handling.
"""
from django.core.exceptions import ValidationError # NOQA
from django.forms.boundfield import * # NOQA
from django.forms.fields import * # NOQA
from django.forms.forms import * # NOQA
from django.forms.formsets import * # NOQA
from django.forms.models import * # NOQA
from django.forms.widgets import * # NOQA
The __init__.py import multiple modules while they are not utilized within the files.
I assume they might be employed by others lived in the same dir, How Django achieve this?
The __init__.py import multiple modules
s/modules/names/ - the from somemodule import somename syntax exposes the somename name, not somemodule.
while they are not utilized within the files.
I assume they might be employed by others lived in the same dir
Actually this is a design pattern known as "facade" - the forms package hides it's inner implementation (in which submodule / subpackage is something defined) so
1/ the users can just import what they need from django.forms without having to care about the underlying modules / subpackages hierarchy,
and
2/ the mainainers can reorganize the underlying modules / subpackages hierarchy without breaking client code.
How Django achieve this?
This is nothing Django specific, it's just plain ordinary Python. Read Python's doc about modules and packages.

How to import every model, function and other things in django project?

I am working on a Django project.
I currently import functions, models like:
from project.abc.models import *
from project.cba.models import *
from project.abc.views import *
from project.cba.views import *
Is there any syntax which can enable me to write just the name of project and it may return every model, functions and etc from every application like:
from project.models import *
from project.views import *
from project.urls import *
I am using Django1.3, and I know that it is not a good practice to import * of anything, but this is my need at the moment. Please help!
I think that it is not a good idea but if you want you can create a file models.py in your project root directory and write there:
from project.abc.models import *
from project.cba.models import *
after that you can import your models like:
from project.models import SomeModel
And you also can create views.py etc.

django import client

i am trying to import the client in django for testing. but when i do, i get this wierd error:
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
this is how i imported the client so that i could use it for testing. can someone explain this to me please.
Try this:
import os
import sys
sys.path.append('/home/username/www/site_folder')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from django.utils import unittest
from django.utils import simplejson as json
from django.test.client import Client
But replace project with folder name, where your settings.py is
The Client is looking for the settings.py. You could simply load the client by typing this in your project folder:
python manage.py shell
In Pycharm which I use, after following this Running Django tests in PyCharm
my problem was solved.
It's in the file > settings > Django Support, and then select the right settings.

Calling a model's method from outside Django

I have a Django model with some static methods. I'd like to call the methods from outside the application (cronjob).
The model I have:
class Job(models.Job):
#Irrelevant information
#staticmethod
def methodIwantToCall():
#statements
I have the following python file that I'm using for the cron job:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from myapp.models import Job
Job.methodIwantToCall()
At first, I was having an error about DJANGO_SETTINGS_MODULE not being set and I fixed that, however, now I have the following error: No module named myapp.utils
I feel like I'm doing something that I'm not supposed to do. So how do I call that static method the way I want it to be called?
EDIT: It looks like the paths are getting messed up when I'm importing from outside Django. For example, I have an import in my models file, when I call the cron file it fails importing with the message ImportError: No module named myapp.utils even though it's working.
The proper solution is to create custom manage.py command.
Assuming your cron job code resides in the same directory as your settings file, use the following setup code at the beginning:
from django.core.management import setup_environ
import settings
setup_environ(settings)

Could not import/No module named Django Error with Apache

I had a small proof of concept set up on a development server on a local machine. I'm now trying to move it over to django on a production server, which I'm using webfaction for. However, now that I'm switched over to apache from the built in django server I get the following:
ViewDoesNotExist: Could not import orgDisplay.views. Error was: No module named orgDisplay.views
But when check my orgDisplay apps folder there is a view.py in it. What am I doing wrong? I've tried adding the following to my settings.py by suggestion of the django IRC room.
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject/orgDisplay")
which is the path to my app.
any ideas on how to even begin to trouble shoot this?
Thanks in advance.
I assume you're using mod_wsgi (which is recommended by Django authors), not mod_python. This is the way you should use your sys.path:
django.wsgi:
import os, sys
sys.path.append(r"/home/user/webapps/django_project/myproject/")
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
sys.stdout = sys.stderr # Prevent crashes upon print
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
urls.py:
from django.conf.urls.defaults import *
urlpatterns = (
("", include("orgDisplay.urls")),
# ...
)
orgDisplay/urls.py:
import views
urlpatterns = (
(r'^some_view/$', views.some_view), # It is actually orgDisplay.views.some_view
# many more records ...
)
It is a bad idea to add project dir itself to path since you're be getting name conflicts between multiple projects.
I think you're appending the wrong directory to sys.path. I think Python is looking in the .../myproject/orgDisplay folder for the orgDisplay package. Try removing the orgDisplay from your string, like this:
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject")
The other option would be to simply add myproject (or whatever your project is actually called) in the import statement.
# instead of "from orgDisplay import views"
from myproject.orgDisplay import views
Also, make sure to restart Apache after every edit.
looking at manage.py, it does it like so:
import sys
from os.path import abspath, dirname, join
from django.core.management import setup_environ
# setup the environment before we start accessing things in the settings.
setup_environ(settings_mod)
sys.path.insert(0, join(PINAX_ROOT, "apps"))
sys.path.insert(0, join(PROJECT_ROOT, "apps"))
Provided your WSGI file is in your project directory, a slightly more flexible way is this:
import os, sys
sys.path.append(os.path.dirname(__file__))
This will enable you to change your project location later without having to modify your WSGI file.