Python 从不同文件夹导入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4383571/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:31:15  来源:igfitidea点击:

Importing files from different folder

pythonimporterrorpython-import

提问by Ivan

I have the following folder structure.

我有以下文件夹结构。

application/app/folder/file.py

and I want to import some functions from file.pyin another Python file which resides in

我想从file.py中的另一个 Python 文件中导入一些函数,文件位于

application/app2/some_folder/some_file.py

I've tried

我试过了

from application.app.folder.file import func_name

and some other various attempts but so far I couldn't manage to import properly. How can I do this?

以及其他一些尝试,但到目前为止我无法正确导入。我怎样才能做到这一点?

回答by Cameron

By default, you can't. When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.pathwhich includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).

默认情况下,您不能。导入文件时,Python 只搜索当前目录、运行入口点脚本的目录以及sys.path包安装目录等位置(实际上比这复杂一点,但这涵盖了大多数情况) .

However, you can add to the Python path at runtime:

但是,您可以在运行时添加到 Python 路径:

# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')

import file

回答by Ax3l

I think an ad-hoc way would be to use the environment variable PYTHONPATHas described in the documentation: Python2, Python3

我认为一种特别的方法是使用文档中描述的环境变量PYTHONPATHPython2, Python3

# Linux & OSX
export PYTHONPATH=$HOME/dirWithScripts/:$PYTHONPATH

# Windows
set PYTHONPATH=C:\path\to\dirWithScripts\;%PYTHONPATH%

回答by joey

Nothing wrong with:

没有错:

from application.app.folder.file import func_name

Just make sure folderalso contains an __init__.py, this allows it to be included as a package. Not sure why the other answers talk about PYTHONPATH.

只需确保folder还包含一个__init__.py,这样就可以将其作为包包含在内。不知道为什么其他答案谈论PYTHONPATH.

回答by Vaibhav Singh

From what I know, add an __init__.pyfile directly in the folder of the functions you want to import will do the job.

据我所知,__init__.py直接在要导入的函数的文件夹中添加一个文件即可完成这项工作。

回答by ChandanK

Considering applicationas the root directory for your python project, create an empty __init__.pyfile in application, appand folderfolders. Then in your some_file.pymake changes as follows to get the definition of func_name:

考虑到application作为根目录为你的Python项目,创建一个空__init__.py文件applicationappfolder文件夹。然后在您some_file.py进行如下更改以获取 func_name 的定义:

import sys
sys.path.insert(0, r'/from/root/directory/application')

from application.app.folder.file import func_name ## You can also use '*' wildcard to import all the functions in file.py file.
func_name()

回答by Timothy C. Quinn

If the purpose of loading a module from a specific path is to assist you during the development of a custom module, you can create a symbolic link in the same folder of the test script that points to the root of the custom module. This module reference will take precedence over any other modules installed of the same name for any script run in that folder.

如果从特定路径加载模块的目的是在自定义模块的开发过程中为您提供帮助,您可以在测试脚本的同一文件夹中创建一个符号链接,指向自定义模块的根目录。对于在该文件夹中运行的任何脚本,此模块引用将优先于安装的任何其他同名模块。

I tested this on Linux but it should work in any modern OS that supports symbolic links.

我在 Linux 上对此进行了测试,但它应该适用于任何支持符号链接的现代操作系统。

One advantage to this approach is that you can you can point to a module that's sitting in your own local SVC branch working copy which can greatly simplify the development cycle time and reduce failure modes of managing different versions of the module.

这种方法的一个优点是您可以指向位于您自己的本地 SVC 分支工作副本中的模块,这可以大大简化开发周期时间并减少管理不同版本模块的故障模式。

回答by Emeeus

This works for me on windows

这在 Windows 上对我有用

# some_file.py on mainApp/app2 
import sys
sys.path.insert(0, sys.path[0]+'\app2')

import some_file

回答by dsg38

Worked for me in python3 on linux

在 linux 上的 python3 中为我工作

import sys  
sys.path.append(pathToFolderContainingScripts)  
from scriptName import functionName #scriptName without .py extension  

回答by slizb

When modules are in parallel locations, as in the question:

当模块位于并行位置时,如问题所示:

application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py

This shorthand makes one module visible to the other:

这个速记使一个模块对另一个可见:

import sys
sys.path.append('../')

回答by CianB

Your problem is that Python is looking in the Python directory for this file and not finding it. You must specify that you are talking about the directory that you are in and not the Python one.

您的问题是 Python 正在 Python 目录中查找此文件,但没有找到。您必须指定您在谈论您所在的目录而不是 Python 目录。

To do this you change this:

要做到这一点,你改变这个:

from application.app.folder.file import func_name

from application.app.folder.file import func_name

to this:

对此:

from .application.app.folder.file import func_name

By adding the dot you are saying look in this folder for the application folder instead of looking in the Python directory.

通过添加点,您是说在此文件夹中查找应用程序文件夹,而不是在 Python 目录中查找。