从目录导入python中的用户定义模块 [英] Importing user defined modules in python from a directory

查看:79
本文介绍了从目录导入python中的用户定义模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入我在python中编写的模块,该模块仅打印出包含数字的列表.我遇到的问题是我希望能够从一个单独的目录中导入它,但是到目前为止,我所读的答案似乎不适用于我的情况.

I'm trying to import a module I wrote in python that just prints out a list containing numbers. The issue I'm having is that I want to be able to import it from a separate directory but the answers I have read so far don't seem to be working for my situation.

例如,假设我要从我的documents文件夹中的目录导入printnumbers.py,我应该实现以下目标:

For example, given I want to import printnumbers.py from a directory in my documents folder I am supposed to implement the following:

    import sys
    sys.path.append('/home/jake/Documents')
    import printnumbers.py

此代码伪装导致导入错误",告诉我指定的模块不存在.我不确定从何处开始,我已经多次检查以确保路径和模块名称的拼写正确.我仍在尝试确切地了解追加到"sys.path"的功能.据我了解,它告诉程序在该目录中搜索模块?

This snipit of code results in a "Import error" telling me that the specified module does not exist. I'm not exactly sure where to proceed from here, I have checked multiple times to make sure it's the right spelling for the path as well as for the module name. I'm still trying to understand exactly what appending to the "sys.path" does. From what I understand it's telling the program to search for modules in that directory?

感谢所有回答我相当新手的问题的人.我只是想对它有一个更好的理解,即python文档并未提供我的思路.

Thanks for anyone who answers my rather novice question. I'm just looking for a better understanding of it that the python documentation isn't providing for my frame of mind.

推荐答案

当文件为printnumbers.py时,该模块称为printnumbers(不带.py).因此使用

When the file is printnumbers.py, the module is called printnumbers (without the .py). Therefore use

import printnumbers


import sys
sys.path.append('/home/jake/Documents')

'/home/jake/Documents'追加到sys.path的末尾.每当import语句使Python搜索模块时,都会搜索(按列出的顺序)在sys.path中列出的目录. (已经导入的模块已缓存在sys.modules中,因此Python并不总是需要搜索sys.path目录来导入模块...)

appends '/home/jake/Documents' to the end of sys.path. The directories listed in sys.path are searched (in the order listed) whenever an import statement causes Python to search for a module. (Already imported modules are cached in sys.modules, so Python does not always need to search sys.path directories to import a module...)

因此,如果您有文件/home/jake/Documents/printnumbers.py,则import printnumbers将导致Python导入它,前提是在/home/jake/Documents/之前的sys.path中列出的目录中没有其他名为printnumbers.py的文件.

So if you have a file /home/jake/Documents/printnumbers.py, then import printnumbers will cause Python to import it provided there is no other file named printnumbers.py in a directory listed in sys.path ahead of /home/jake/Documents/.

请注意,将目录注入到sys.path中不是设置Python搜索模块的常用方法.通常,最好将/home/jake/Documents添加到您的PYTHONPATH环境变量中. sys.path将自动包括PYTHONPATH环境变量中列出的目录.

Note that injecting directories into sys.path is not the usual way to set up Python to search for modules. Usually it is preferable to add /home/jake/Documents to your PYTHONPATH environment variable. sys.path will automatically include the directories listed in the PYTHONPATH environment variable.

这篇关于从目录导入python中的用户定义模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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