【问题标题】:import module from other folder python (errors)从其他文件夹 python 导入模块(错误)
【发布时间】:2019-05-19 12:54:30
【问题描述】:

我是 Python 新手。我正在使用以下目录结构并尝试将模块 OrgRepo 导入 Func1。我正在使用 virtualenv 和 vs 代码作为我的 IDE。

src/ ├── Functions │   ├── Func1 │   └── Func2 └── Shared ├── __init__.py ├── Repositories │ ├── __init__.py │   ├── OrgRepo └── Utilities ├── __init__.py └── DictUtil 我也试过没有`init.py'

这是我的路径:

['/Users/username/Documents/Projects/myproject/name/src/Functions/Func1', '/Users/username/anaconda3/envs/my_virtual_env/lib/python37.zip', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7/lib-dynload', '/Users/username/.local/lib/python3.7/site-packages', '/Users/username/anaconda3/envs/my_virtual_env/lib/python3.7/site-packages']

为了将 OrgRepo 导入 Func1,我尝试了以下方法:

1:from .Shared.Repositories import OrgRepo

ModuleNotFoundError: No module named '__main__.Shared'; '__main__' is not a package

2:from ..Shared.Repositories import OrgRepo '

ValueError: attempted relative import beyond top-level package

3:from src.Shared.Repositories import OrgRepo

ModuleNotFoundError: No module named 'src'

4: `从 Shared.Repositories 导入 OrgRepo1

'ModuleNotFoundError: No module named 'Shared'

5:我正在使用 VS Code,当我尝试保存文件时:

它会自动更改为: import OrgRepo import DictionaryUtilities import datetime import json import sys sys.path.insert(0, 'src/Repositories/')

6: import sys sys.path.insert( 0, '/Users/username/Documents/Projects/project/m/src/Repositories') import OrgRepo

还有这个:

sys.path.insert(0, 'Repositories') sys.path.insert(0, .'Repositories') sys.path.insert(0, ..'Repositories')

运行或保存时,vs 代码将其更改为: import OrgRepo import sys sys.path.insert( 0, '/Users/username/Documents/Projects/project/m/src/Repositories') 并收到此错误:

ModuleNotFoundError: No module named 'OrgRepo'

我可以使用 PIP 安装并导入它,但这不符合我们的需要。

我已阅读以下帖子: Importing files from different folder

Python import modules, folder structures

How to Import Multiple Python Modules from Other Directories

How to properly import python modules from an adjacent folder?

我也尝试阅读/理解其他一些帖子。 . .我什至试图敲打助焊剂电容器几次,但无济于事。 .

编辑:我正在使用此代码作为 AWS Lambda 函数上传。虽然 sys.path 解决方案在本地工作,但它不适合我的工作流程。这需要我在上传时更改要导入的 sys.path,这会导致 Intellisense 出现问题。我希望能够直接导入模块。例如import OrgRepo 所以 Intellisense 不会抛出错误,我可以压缩包并将其上传到 AWS。当我能够导入 <module_name> 时,我将包上传到 AWS 没有问题。

我在 Anaconda 中激活我的环境,然后导出以下 PYTHONPATH 环境变量:

export PYTHONPATH=src/shared/repositories:src/shared/utilities

我也试过export PYTHONPATH=$PATH:src/shared/repositories:src/shared/utilities

这工作了一段时间,现在我通过 IntelliSense 获得了PYTHON[unresolved-import]。当我尝试从/src 上方的目录运行脚本时,我似乎没有收到此错误。

如果有人能告诉我如何使用标准 import <module> 导入我的模块并让它始终如一地工作,我将不胜感激。

【问题讨论】:

  • OrgRepo 是 python 文件还是目录?
  • 你检查过你的 $PYTHONPATH 环境变量了吗?它的价值是什么?
  • OrgRepo 被重命名为 org_repo.py,它是一个文件。路径为 src/Shared/Repositories.org_repo.py
  • $PYTHONPATH 为空。
  • $PYTHONPATH =src/shared/utilities/:src/shared/repositories:src/shared/services:src/lambda_functions/get_pixel

标签: python python-import importerror


【解决方案1】:

基本上就是说不能导入目录,但是可以导入目录中的文件。

假设您在 OrgRepo 中有 Org.py 文件,您可以这样做:

from src.Shared.Repositories.OrgRepo import Org

或者,如果您想从中调用特定方法,可以说do_it:

from src.Shared.Repositories.OrgRepo.Org import do_it

Read more

【讨论】:

  • 我的印象是,如果您在导入其中文件(或文件子集,甚至来自特定文件的函数子集)?
  • 那也行不通。我需要将这些作为包上传到 lambda 函数,并且需要能够在本地进行编辑。有一次,我设置了我的 PYTHONPATH 并且 vs 代码能够识别本地模块并且能够从任何目录运行。
  • 这应该是一个非常容易解决的问题。这是一个 PYTHON PATH 变量。
【解决方案2】:

我认为您正在尝试执行以下操作。我已经清理了一些目录名称(通常目录是带有下划线的小写字母,类名称是大写字母),在 python 文件中添加了.py 扩展名,并尝试创建一个简约的环境来复制您的场景。希望这会有所帮助。

设置环境

$ mkdir src; mkdir src/functions; touch src/functions/func1.py; mkdir src/shared; mkdir src/shared/repositories; touch src/shared/repositories/org_repo.py

$ tree
.
└── src
    ├── functions
    │   └── func1.py
    └── shared
        └── repositories
            └── org_repo.py

# src/shared/repositories/org_repo.py
def a_cool_func():
    print(f'hello from {__file__}')

# src/functions/func1.py
import pathlib
import sys

file_path = pathlib.Path(__file__)
path = file_path.resolve().parents[2]
sys.path.append(str(path))

from src.shared.repositories.org_repo import a_cool_func

print(f'hello from {__file__}')

a_cool_func()

运行它

# note that there is no significance to /Users/username/tmp/tmp/
# this is just the directory where the test environment was setup
$ python src/functions/func1.py
hello from src/functions/func1.py
hello from /Users/username/tmp/tmp/src/shared/repositories/org_repo.py

【讨论】:

  • @DavidAndrews 太好了。如果它对您有用,请接受答案。如果您在一个模块中有多个功能,那么您可以from dir1.dir2.module_name import func1, func2
  • 我做了,但我还没有足够的声望票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 2021-07-16
  • 2013-08-26
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
相关资源
最近更新 更多