使Python在我的目录中使用代码(而不是在/usr/...中) [英] Making Python Use Code in My Directory (not that in /usr/...)

查看:99
本文介绍了使Python在我的目录中使用代码(而不是在/usr/...中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的(Ubuntu)系统上已经安装的Python库上工作.我检出了该库,编辑了一些文件,并编写了一个小脚本来测试我的更改.即使我将脚本与库文件放在同一文件夹中,似乎Python仍在使用已安装的版本(/usr/share/pyshared/...中的版本).

I am trying to work on a Python library that is already installed on my (Ubuntu) system. I checked out that library, edited some files, and wrote a small script to test my changes. Even though I put my script in the same folder as that of the library, it seems Python is using the installed version instead (the one in /usr/share/pyshared/...).

这是我的目录结构:

src
+ my_package
    - my_script.py
+ library_package
    - lots_of_code

如何告诉Python使用目录中的代码,而不是已安装的代码?

How can I tell Python to use the code in my directory, not the installed one?

推荐答案

您可以使用PYTHONPATH环境变量指定python在何处搜索模块:

You can dictate where python searches for modules using the PYTHONPATH environment variable:

导入名为垃圾邮件的模块时, 解释器搜索文件 当前名为spam.py 目录,然后在列表中 由指定的目录 环境变量PYTHONPATH.这 与shell具有相同的语法 变量PATH,即 目录名称.当PYTHONPATH为 未设置或找不到文件时 在那里,搜索继续 与安装有关的默认路径; 在Unix上,通常是 .:/usr/local/lib/python.

When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python.

来自 http://docs.python.org/tutorial/modules.html#the-module-search-path

这篇关于使Python在我的目录中使用代码(而不是在/usr/...中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆