我有这样的文件夹结构:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
如何从file.py或some_file.py中导入函数?我尝试了:
from application.app.folder.file import func_name
但它不起作用。
我有这样的文件夹结构:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
如何从file.py或some_file.py中导入函数?我尝试了:
from application.app.folder.file import func_name
但它不起作用。
当前回答
当模块位于平行位置时,如问题所示:
application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py
这种速记使一个模块对另一个模块可见:
import sys
sys.path.append('../')
其他回答
我很特别:我在Windows中使用Python!
我只完成了信息:对于Windows和Linux,相对路径和绝对路径都可以在sys.path中工作(我需要相对路径,因为我在几台电脑上和不同的主目录下使用脚本)。
当使用Windows时,\和/都可以用作文件名的分隔符,当然,您必须将\加倍为Python字符串,一些有效的例子:
sys.path.append('c:\\tools\\mydir')
sys.path.append('..\\mytools')
sys.path.append('c:/tools/mydir')
sys.path.append('../mytools')
(注意:我认为/比\更方便,如果它不是“Windows本机”,因为它与Linux兼容,并且更容易写入和复制到Windows资源管理器)
├───root
│ ├───dir_a
│ │ ├───file_a.py
│ │ └───file_xx.py
│ ├───dir_b
│ │ ├───file_b.py
│ │ └───file_yy.py
│ ├───dir_c
│ └───dir_n
您可以将父目录添加到PYTHONPATH,为了实现这一点,您可以在sys.path中列出的“模块搜索路径”中使用依赖于操作系统的路径。因此,您可以像下面这样轻松地添加父目录:
# file_b.py
import sys
sys.path.insert(0, '..')
from dir_a.file_a import func_name
我正在处理项目a,我希望用户通过pip-install a使用以下文件列表进行安装:
.
├── setup.py
├── MANIFEST.in
└── a
├── __init__.py
├── a.py
└── b
├── __init__.py
└── b.py
设置.py
from setuptools import setup
setup (
name='a',
version='0.0.1',
packages=['a'],
package_data={
'a': ['b/*'],
},
)
歧管in
recursive-include b *.*
a/init.py
from __future__ import absolute_import
from a.a import cats
import a.b
a/a.py
cats = 0
a/b/init.py
from __future__ import absolute_import
from a.b.b import dogs
a/b/b.py
dogs = 1
我通过在MANIFEST.in目录下运行以下命令来安装模块:
python setup.py install
然后,在我的文件系统上的一个完全不同的位置上,我可以运行:
import a
dir(a)
这证实了a.ats确实等于0,a.b.dogs确实等于1,正如预期的那样。
我多次遇到同一个问题,所以我想分享我的解决方案。
Python版本:3.X
以下解决方案适用于在Python 3.X版本中开发应用程序的人,因为自2020年1月1日以来,Python 2不受支持。
项目结构
在python3中,由于隐式命名空间包,项目子目录中不需要__init__.py。请参见Python 3.3中的包是否不需要init.py+
Project
├── main.py
├── .gitignore
|
├── a
| └── file_a.py
|
└── b
└── file_b.py
问题陈述
在file_b.py中,我想在文件夹a下的file_a.py中导入一个类a。
解决
#1快速但肮脏的方式
不安装软件包,就像您当前正在开发一个新项目
使用try-catch检查是否存在错误。代码示例:
import sys
try:
# The insertion index should be 1 because index 0 is this file
sys.path.insert(1, '/absolute/path/to/folder/a') # the type of path is string
# because the system path already have the absolute path to folder a
# so it can recognize file_a.py while searching
from file_a import A
except (ModuleNotFoundError, ImportError) as e:
print("{} fileure".format(type(e)))
else:
print("Import succeeded")
#2安装软件包
安装应用程序后(在本文中,不包括安装教程)
你可以简单地
try:
from __future__ import absolute_import
# now it can reach class A of file_a.py in folder a
# by relative import
from ..a.file_a import A
except (ModuleNotFoundError, ImportError) as e:
print("{} fileure".format(type(e)))
else:
print("Import succeeded")
编码愉快!
当模块位于平行位置时,如问题所示:
application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py
这种速记使一个模块对另一个模块可见:
import sys
sys.path.append('../')