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

编程入门 行业动态 更新时间:2024-10-27 10:28:25
从目录中导入python中的用户定义模块(Importing user defined modules in python from a directory)

我试图导入一个我用python编写的模块,它只打印出一个包含数字的列表。 我遇到的问题是我希望能够从一个单独的目录中导入它,但迄今为止我阅读的答案似乎不适合我的情况。

例如,假设我想从我的文档文件夹中的一个目录中导入printnumbers.py,我应该执行以下操作:

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

这段代码导致“导入错误”告诉​​我指定的模块不存在。 我不确定从哪里开始,我已经多次检查过,以确保它是正确的路径拼写以及模块名称。 我仍然试图了解到什么附加到“sys.path”。 据我所知,它告诉程序在该目录中搜索模块?

感谢任何回答我的新手问题的人。 我只是在寻找更好的理解,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.

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

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?

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 )。 因此使用

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

将'/home/jake/Documents'附加到sys.path的末尾。 只要导入语句导致Python搜索模块,就会搜索sys.path中列出的目录(按所列顺序)。 (已导入的模块已缓存在sys.modules ,因此Python并不总是需要搜索sys.path目录才能导入模块...)

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

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

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')

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...)

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/.

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.

更多推荐

本文发布于:2023-07-15 11:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   定义   用户   目录中   modules

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!