如何通过名称导入自定义python包 [英] How to import custom python package by name

查看:118
本文介绍了如何通过名称导入自定义python包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个名为"custom_module"的文件夹,并且在包含以下内容的文件夹中有__init __.py:

I have created a folder named "custom_module" and I have the __init__.py inside the folder which contains:

__all__ = [
        'Submodule1',
        'Submodule2'
]

从我阅读的文档中,我应该可以调用import custom_module并获得对该程序包的访问权限,但是这没有发生.如何让python识别我的包裹?我正在使用python 3.2

From what documentation I read I should be able to call import custom_module and get access to the package, however this isn't happening. How can I make python recognize my package? I am using python 3.2

更新: 该软件包不在python文件夹中. python环境如何找到它,因此我可以按名称成功导入它.

Update: The package is not located in the python folder. How does the python environment find it, so I can successfully import it by name.

推荐答案

sys.path保存Python搜索路径.在尝试import模块和软件包之前,将其设置为包含您的路径:

sys.path holds the Python search path. Before trying to import your modules and packages, set it to include your path:

import sys
sys.path.insert(0, 'your_path_here')
import custom_module

Python文档和此问题

这篇关于如何通过名称导入自定义python包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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