AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 28216
Accepted
Bunny Rabbit
Bunny Rabbit
Asked: 2011-02-27 09:46:16 +0800 CST2011-02-27 09:46:16 +0800 CST 2011-02-27 09:46:16 +0800 CST

设置 Django 主干版本的 Python 路径

  • 772

我正在尝试为 Django 的主干版本运行单元测试。我的系统上还安装了一个 Django 版本,我通过apt-get install.

如何更改 Python 路径以指向 Django 的主干版本?

python
  • 2 2 个回答
  • 5504 Views

2 个回答

  • Voted
  1. Best Answer
    belacqua
    2011-02-27T10:49:38+08:002011-02-27T10:49:38+08:00

    从 bash(或其他 shell)中,您可以操纵 $PYTHONPATH 以指向父目录,例如,
    echo $PYTHONPATH
    export PYTHONPATH="/newhome/django_x.x/trunk"

    这将预先给现有的 python 路径列表提供路径。

    蟒蛇.org:

    例如,如果 PYTHONPATH 设置为 /www/python:/opt/py,则搜索路径将以

    ['/www/python','/opt/py']
    

    (请注意,目录必须存在才能添加到 sys.path;站点模块会删除不存在的路径。)

    在 python 中,您还可以更改 sys.path (import sys如果没有)以指向您的测试分支。

    来自python.org的示例:

    $ python
    Python 2.2 (#11, Oct  3 2002, 13:31:27)
    [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
     '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
     '/usr/local/lib/python2.3/site-packages']
    >>>
    

    要添加路径,请使用以下内容:

    sys.path.append('/workingdir/python/')
    

    来自djangotricks(博客)的以 Django 为中心的示例:

    import os, sys
    
    SVN_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
    DJANGO_PATH = os.path.join(SVN_PATH, "django_src", "trunk")
    PROJECT_PATH = os.path.join(SVN_PATH, "myproject", "trunk")
    
    sys.path += [DJANGO_PATH, PROJECT_PATH]
    

    此外,除非您抑制了该行为,否则 python 会site.py在执行时加载,因此您还可以编辑site.py文件,例如/usr/lib/python2.7/site.py:

    该site.py文件的注释也具有指导意义。

    $ more /usr/lib/python2.7/site.py
    
    """Append module search paths for third-party packages to sys.path.
    
    ****************************************************************
    * This module is automatically imported during initialization. *
    ****************************************************************
    
    In earlier versions of Python (up to 1.5a3), scripts or modules that
    needed to use site-specific modules would place ``import site''
    somewhere near the top of their code.  Because of the automatic
    import, this is no longer necessary (but code that does it still
    works).
    
    This will append site-specific paths to the module search path.  On
    Unix (including Mac OSX), it starts with sys.prefix and
    sys.exec_prefix (if different) and appends
    lib/python<version>/site-packages as well as lib/site-python.
    On other platforms (such as Windows), it tries each of the
    prefixes directly, as well as with lib/site-packages appended.  The
    resulting directories, if they exist, are appended to sys.path, and
    also inspected for path configuration files.
    
    For Debian and derivatives, this sys.path is augmented with directories
    for packages distributed within the distribution. Local addons go
    into /usr/local/lib/python<version>/dist-packages,
    Debian addons
    install into /usr/{lib,share}/python<version>/dist-packages.
    /usr/lib/python<version>/site-packages
    is not used.
    
    A path configuration file is a file whose name has the form
    <package>.pth; its contents are additional directories (one per line)
    to be added to sys.path.  Non-existing directories (or
    non-directories) are never added to sys.path; no directory is added to
    sys.path more than once.  Blank lines and lines beginning with
    '#' are skipped. Lines starting with 'import' are executed.
    
    For example, suppose sys.prefix and sys.exec_prefix are set to
    /usr/local and there is a directory
    /usr/local/lib/python2.5/site-packages
    with three subdirectories, foo, bar and spam, and two path
    configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
    following:
    
      # foo package configuration
      foo
      bar
      bletch
    
    and bar.pth contains:
    
      # bar package configuration
      bar
    
    Then the following directories are added to sys.path, in this order:
    
      /usr/local/lib/python2.5/site-packages/bar
      /usr/local/lib/python2.5/site-packages/foo
    
    Note that bletch is omitted because it doesn't exist; bar precedes
    foo
    because bar.pth comes alphabetically before foo.pth; and
    spam is
    omitted because it is not mentioned in either path configuration
    file.
    
    After these path manipulations, an attempt is made to import a module
    named sitecustomize, which can perform arbitrary additional
    site-specific customizations.  If this import fails with an
    ImportError exception, it is silently ignored.
    

    参考:
    python.org 文档,v2.7 搜索路径
    python.org 文档,v3 搜索路径
    Djangotricks 博客,关于 python 路径的注释
    Martin Jansen:用于开发和生产的 Django 设置文件

    • 6
  2. myusuf3
    2011-02-27T10:49:39+08:002011-02-27T10:49:39+08:00

    好问题。你可以用python解决这个问题。打开python解释器和

    Python 2.6
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> print sys.path
    ['', '/usr/lib/python24.zip', '/usr/lib/python2.4',
    '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk',
    '/usr/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages',
    '/usr/lib/python2.4/site-packages', '/usr/lib/site-python']
    

    要更改路径,只需将条目附加/前置到 sys.path,这是一个普通的 Python 列表,例如

    >>> sys.path.append('/path/to/django/trunk')
    >>> sys.path.insert(0, '/path/to/django/trunk')
    

    这些命令中的任何一个都应将其添加到您的路径中。

    • 1

相关问题

  • 默认的字符编码是什么?

  • 如何使用 pynotify 创建可点击通知?

  • 有没有安装 Django 1.2.*(最新稳定版)的简单方法?

  • 为 Python 应用程序设置构建系统

  • 为我的 PPA 创建包时遇到问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve