How to Add Locations to Python Path for reusable D

Source: Internet
Author: User

In my previous post I talk about the reusable apps, but I don ' t really explain it's that much. If you have a app that might is useful in another project, it's best to not refer to the project name in the application So you don ' t has to search and remove it when adding to another project. To never refer to your project name in your app's code, you'll need to put your app on the Python path. I usually doproject_folder/apps/app_folder so apps would need to being a location this python is checking wh En you is importing so this importing looks like the following:

From Appname.filename import foo

There is a few places you might need to add an Apps folder to the Pythonpath.

Add to settings.py

This would add the apps directory in your project directory to the beginning of the path list. This would allow manage.py syncdb and manage.py runserver to know that the Apps folder should be added.

Import OS import sys project_root = Os.path.dirname (__file__) sys.path.insert (0, Os.path.join (project_root, "apps"))

That should is all need to does to get the most Django projects working with your reusable apps and if for any reason you n Eed add to the path with mod python or mod wsgi, the following should work.



Apache Mod_python

in the   setting-up-everything post   I Show an example httpd.conf file. In your Apache configuration you'll probably see something similar to what's below. To add,   /var/www/myproject/apps   to the PythonPath I added it In the list.

SetHandler Python-programpythonhandler DJANGO.CORE.HANDLERS.MODPYTHONSETENV Django_settings_module Myproject.settingspythonoption django.root/myprojectpythondebug Onpythonpath "['/var/www ', '/var/www/myproject/ Apps '] + Sys.path "
Apache Mod_wsgi

If you use mod wsgi instead of mod python, your Apache config would be loading a WSGI file with a line like this C1>wsgiscriptalias/var/www/myproject/myproject.wsgi. You'll need to edit this file to add to the path (Django's site has a example file).

Sys.path.append ('/var/www/myproject/apps ')

You never know if you might want-to-use a app in another project, so always try-to-keep from mentioning the project Nam e anywhere in the applications.


How to Add Locations to Python Path for reusable D

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.