How to Add Locations to Python Path for Reusable D

In myprevious post I talk about reusable apps, but I don’t really explain it that much. If you have an app that might be useful in another project, it’s best to not refer to the project name in the application so you don’t have to search and remove it when adding to another project. To never refer to your project name in your app’s code, you will need to put your app on the python path. I usually doproject_folder/apps/app_folder so apps will need to be a location that python is checking when you are importing so that importing looks like the following:

from appname.filename import foo

There are a few places you might need to add an apps folder to the pythonpath.

Add to settings.py

This will add the apps directory in your project directory to the beginning of the path list. This will 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 be all you need to do to get most django projects working with your reusable apps, but if for any reason you need 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 will probably see something similar to what is below. To add the location /var/www/myproject/apps to the PythonPath I added it in the list.

SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
PythonOption django.root /myproject
PythonDebug On
PythonPath "[‘/var/www‘,‘/var/www/myproject/apps‘] + sys.path"

Apache mod_wsgi

If you use mod wsgi instead of mod python, your apache config will be loading a wsgi file with a line like this WSGIScriptAlias /var/www/myproject/myproject.wsgi. You will need to edit that file to add to the path (django’s site has an example file).

sys.path.append(‘/var/www/myproject/apps‘)

You never know when you might want to use an app in another project, so always try to keep from mentioning the project name anywhere in the applications.

时间: 2024-10-19 15:49:01

How to Add Locations to Python Path for Reusable D的相关文章

python path

import os print os.path.relpath('/home/work/compasses_liantiao/auto-opt/framework') print os.path.abspath('../../') print os.path.exists("/home/work") print os.path.exists("./test_path.py") print os.path.isfile("/home/work")

Leetcode 解题 Add Two Numbers Python

原题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit Add the two numbers and return it as a linked list Input: (2->4->3) + (5->6->4) Out

67. Add Binary [easy] (Python)

题目链接 https://leetcode.com/problems/add-binary/ 题目原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题目翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如:a = "11",b = &q

leetcode add two numbers python

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode &q

leetcode 241. Different Ways to Add Parentheses (Python版)

题目: Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+, - and *. 大意是给定一个运算,求解所有运算序列的解 例如 Input: "2*3-4*5" (2*(3-(4*5))) = -

67. Add Binary Leetcode Python

For example, a = "11" b = "1" Return "100". 这题的做法和前面一题plus one 一样,区别在于需要将两个string先相加,再判断是否要进位. 直接上代码吧. class Solution: # @param a, a string # @param b, a string # @return a string def addBinary(self, a, b): a=list(a) b=list(b

《跟老齐学python Django实战 》笔记

安装 Django是以Python为语言环境的,所以要先确保计算机上已经安装了Python. Linux ubuntu: sudo pip install Django==1.11.7 安装中指定了版本,可以修改版本号,或者不指定将安装软件源中已有的版本. 安装完成后,可以进入到Python交互模式中查看版本: >>> import django >>> print(django.get_version()) 1.11.7 上面是简单而常用的方法,除了这种方法,还可以下

python +djanjo 开发

1.安装好python运行环境这里使用最新版python3.7.0+Django==2.0.7,安装完成后 Django 管理工具 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.py.我们可以使用 django-admin.py 来创建一个项目: 我们可以来看下django-admin.py的命令介绍: [[email protected] ~]# django-admin.py Usage: django-admin.py subcommand [opt

Django框架学习

Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由Python写成. Django遵守BSD版权,初次发布于2005年7月, 并于2008年9月发布了第一个正式版本1.0 . Django采用了MVC的软件设计模式,即模型M,视图V和控制器C. Django 安装 在安装 Django 前,系统需要已经安装了Python的开发环境.接下来我们来具体看下不同系统下Dja