美文网首页
import python file from another

import python file from another

作者: 夏天不热也不冷 | 来源:发表于2020-02-02 19:55 被阅读0次

有的时候会遇到现在写的程序想调用之前写好的Python包的情况,但是两个文件并不在同一个文件加上,如果复制的话感觉会很乱,并且会占用空间,所以在调用的时候就直接可以调用其他文件夹的Python包。

但是在import file 的时候,Python只在当前的文件夹里面search。

但是可以在run程序的时候加进去路径名

import sys
sys.path.insert(1,'another folder name')
import file # file in another folder

还有另一种方式是当folder里面有 __ init__.py文件的时候,这时候就可以当做是Python package import了,如果说package位于‘ashley/note/python/file.py’

from ashley.note.python.file import function_name

以上就是我找到的方法,参考网址:https://stackoverflow.com/questions/4383571/importing-files-from-different-folder

相关文章

网友评论

      本文标题:import python file from another

      本文链接:https://www.haomeiwen.com/subject/nyoqxhtx.html