如何从 Python 中的其他项目导入函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14509192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import functions from other projects in Python?
提问by Big Dogg
I have some code in a project which I'd like to reuse in another project. What do I need to do (in both folders) so that I can do this?
我在一个项目中有一些代码,我想在另一个项目中重用。我需要做什么(在两个文件夹中)才能做到这一点?
The directory structure is something like:
目录结构类似于:
- Foo
- Project1
- file1.py
- file2.py
- Project1
- Bar
- Project2
- fileX.py
- fileY.py
- Project2
- 富
- 项目1
- 文件1.py
- 文件2.py
- 项目1
- 酒吧
- 项目2
- 文件X.py
- 文件Y.py
- 项目2
I want to use functions from file1.py and file2.py in fileX.py and fileY.py.
我想在fileX.py 和fileY.py 中使用file1.py 和file2.py 中的函数。
采纳答案by Rob Cowie
Ideally both projects will be an installable python package, replete with __init__.py and setup.py. They could then be installed with python setup.py installor similar.
理想情况下,这两个项目都是一个可安装的 python 包,充满了 __init__.py 和 setup.py。然后可以安装它们python setup.py install或类似的。
If that is not possible, don'tuse execfile()! Manipulate the PYTHONPATHto add Fooso that import Project1.file1works.
如果这是不可能的,请不要使用execfile()!操作PYTHONPATH以添加Foo以便import Project1.file1工作。
For example, from Project2/fileX.py:
例如,来自 Project2/fileX.py:
from os import path
import sys
sys.path.append(path.abspath('../Foo'))
from Project1.file1 import something
However, the realanswer is to make each a discrete installable package.
然而,真正的答案是使每个都成为一个独立的可安装包。
回答by msc
回答by Bibhas Debnath
I think you can add Footo the current Python path using os.path.join()or os.path.append()and do from import Project1.file1 import function_name.
我认为您可以Foo使用os.path.join()oros.path.append()和 do添加到当前的 Python 路径中from import Project1.file1 import function_name。
回答by Lennart Regebro
You take the code you want to use in both projects, and you put it into a module, which you extract into a third separate project. That project you make into a package, which you can work on separately. You then release version of it, and reuse them in your other projects.
您将要在两个项目中使用的代码放入一个模块中,然后将其提取到第三个单独的项目中。你把那个项目打包成一个包,你可以单独处理。然后您发布它的版本,并在您的其他项目中重用它们。
It is important that you have versions that you "release" so that you can keep track of which version of the module each project uses. Otherwise you will end up in a situation where an old project stops working because you have made incompatible changes to the common module.
重要的是你有你“发布”的版本,这样你就可以跟踪每个项目使用的模块版本。否则,您最终会遇到旧项目停止工作的情况,因为您对公共模块进行了不兼容的更改。
If it's generically usable not only for you but for others, consider uploading it to PyPI.
如果它不仅对您而且对其他人都通用,请考虑将其上传到PyPI。
回答by Ryan Haining
There's a lot going on here. you should read about python packages and module management http://docs.python.org/2/tutorial/modules.html#packagesbut the basic idea is that fileX needs to know where file1 and file2 are in order to use them.
这里发生了很多事情。你应该阅读关于 python 包和模块管理http://docs.python.org/2/tutorial/modules.html#packages但基本思想是 fileX 需要知道 file1 和 file2 在哪里才能使用它们。
To turn a folder into a package, it just needs to contain an __init__.pyfile. What I would suggest you do is (in a terminal)
要将文件夹变成包,它只需要包含一个__init__.py文件。我建议你做的是(在终端中)
$ touch Foo/__init__.py
$ touch Foo/Project1/__init__.py
(assuming you're using unix/linux).
(假设您使用的是 unix/linux)。
Then somehow, fileX needs to know where the Foopackage is. You can call sys.path.append(PATH)where PATH is the location of Foo.
然后不知何故,fileX 需要知道Foo包在哪里。您可以调用sys.path.append(PATH)其中 PATH 是 Foo 的位置。
finally inside fileX.py you'd have
最后在 fileX.py 里面你会有
import sys
sys.path.append(PATH) #replace PATH with the path to Foo
from Foo.Project1 import file1
#use its functions
file1.function_name(argument)
if you really want to just say function_namewithout the preceeding file1.you can import all of its functions by saying from Foo.Project1.file1 import *however pleasenote that from module import *is highlyfrowned upon as it mixes names and make code less readable and understandable
如果你真的只想说function_name没有前面的file1.你可以通过说导入它的所有功能from Foo.Project1.file1 import *但是请注意这from module import *是非常不受欢迎的,因为它混合了名称并使代码更难阅读和理解

