简体   繁体   English

Python中的模块路径

[英]Modules paths in Python

I have created a folder with all my modules for my GAE application and with external libraries like Jinja2 to keep everything sorted in one place. 我已经为我的GAE应用程序创建了一个包含所有模块的文件夹,并使用像Jinja2这样的外部库来保存所有内容。 I have folders structure like this: 我有这样的文件夹结构:

lib\
   \utils\
         \__init__.py
   \firepython
   \jinja2
   \jsonpickle
   __init__.py
   sessions.py

When I try to load Jinja from utils__init__.py, I get error ImportError: No module named jinja2.environment . 当我尝试从utils__init__.py加载Jinja时,我收到错误ImportError: No module named jinja2.environment When I look at Jinja2 imports instructions, I see them look like from jinja2.loaders . 当我查看Jinja2导入说明时,我看到它们看起来像是from jinja2.loaders I try to change them to be like from lib.jinja2.loaders but some other errors then appear about imports. 我尝试将它们更改为类似于from lib.jinja2.loaders但是出现了一些关于导入的其他错误。 More than that I don't think it's a good practice to change these imports in external libraries source if there is a more convenient and right way to import modules properly. 更重要的是,如果有一种更方便,更正确的方式正确导入模块,我认为在外部库源中更改这些导入是不错的做法。 I also have added some paths to PYTHONPATH but it doesn't solve all problems. 我也为PYTHONPATH添加了一些路径,但它并没有解决所有问题。 How can I properly import an external package that is placed in another folder, may be with a deep structure? 如何正确导入放置在另一个文件夹中的外部包,可能具有深层结构?

Indeed you should not have to change imports in external libraries - though depending on your environment, you might even have too. 实际上,您不必更改外部库中的导入 - 尽管取决于您的环境,您甚至可能也有。

PYTHONPATH PYTHONPATH

Modifying your PYTHONPATH should suffice; 修改你的PYTHONPATH应该足够了; PYTHONPATH should contain a 'lib' path that is either absolute or relative to your home, eg. PYTHONPATH应该包含一个'lib'路径,它是绝对的或相对于你的家,例如。

Then you could simply do 然后你就可以做到

from jinja2 import WHATEVER

sys.path.append sys.path.append

Another way to go without PYTHONPATH is to use sys.path.append() and add your paths from your python code. 没有PYTHONPATH的另一种方法是使用sys.path.append()并从python代码中添加路径。 I actually favor that, as it also allows to have per-application paths. 我实际上赞成这一点,因为它也允许每个应用程序路径。

use virtualenv 使用virtualenv

Details would be a bit long to be put here, but please follow the official doc 详细信息可能会有点长,但请遵循官方文档

These options applies to general python development rather than GAE specificities; 这些选项适用于一般的python开发而不是GAE特性; if it does not work on your development machine you should post more details (exact imports, absolute paths, pythonpath...). 如果它在您的开发机器上不起作用,您应该发布更多细节(精确导入,绝对路径,pythonpath ......)。

A proper project structure and use of appcfg.py should workout dependencies when uploading to google: please take a look at this good answer: How do I manage third-party Python libraries with Google App Engine? 上传到谷歌时,正确的项目结构和appcfg.py的使用应该是锻炼依赖性:请看一下这个好答案: 如何使用Google App Engine管理第三方Python库? (virtualenv? pip?) and follow those guidelines. (virtualenv?pip?)并遵循这些准则。

A nice way to go with GAE is through yaml application directives. 使用GAE的一个好方法是通过yaml应用程序指令。 Please take a look at the doc for includes: http://code.google.com/appengine/docs/python/config/appconfig.html#Includes 请查看包含以下内容的文档: http//code.google.com/appengine/docs/python/config/appconfig.html#Includes

Also remember that GAE officially supports python 2.5, and 2.7 support is experimental 还记得GAE 正式支持 python 2.5,而2.7支持 是实验性的

Python 2.7 is now officially supported Python 2.7现在已正式支持

To properly import a module, you need to make sure, that python knows where to find it. 要正确导入模块,您需要确保python知道在哪里找到它。 To do so, for each external library append it's parent directory to the sys.path (in run-time), or setup PYTHONPATH environment (before running). 为此,每个外部库将其父目录附加到sys.path(在运行时)或设置PYTHONPATH环境(在运行之前)。

For example: 例如:

import sys
sys.path.append('/my/lib')

# now we can import from lib
import jsonpickle # will load /my/lib/jsonpickle/__init__.py

See http://docs.python.org/tutorial/modules.html#the-module-search-path . 请参阅http://docs.python.org/tutorial/modules.html#the-module-search-path to understand what python does when you call import. 了解调用import时python的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM