python - 无论如何强制 PyCharm 总是使用绝对导入?

标签 python pycharm

我使用的是 PyCharm,经常依赖 alt+enter 快捷键来自动导入类和函数。

但是,它不使用绝对导入路径。它在本地运行良好,但是当我推送到 GitHub 时,我在 TravisCI 中的测试失败了。

有谁知道强制 PyCharm 使用绝对路径导入的方法吗?

我需要像这样导入 drone_squadron.api.drone_api 如果我使用像这样的 api.drone_api 远程测试找不到导入。这适用于所有本地进口。

我希望所有导入始终都是绝对的。相关进口导致我在打包项目时遇到问题。我认为一直使用绝对导入更容易。

Git 仓库

https://github.com/sarcoma/drone_squadron_api_prototype

树结构

.
├── coverage.xml
├── LICENSE.md
├── pytest.ini
├── README.md
├── requirements.txt
├── drone_squadron
│   ├── app.py
│   ├── endpoints.http
│   ├── flask.cfg
│   ├── __init__.py
│   ├── load_fixtures.py
│   ├── main.py
│   ├── router.py
│   ├── schema.py
│   ├── test_flask.cfg
│   ├── api
│   │   ├── base_api.py
│   │   ├── drone_api.py
│   │   ├── gimbal_api.py
│   │   ├── __init__.py
│   │   ├── price_api.py
│   │   ├── round_type_api.py
│   │   ├── scanner_api.py
│   │   ├── squadron_api.py
│   │   ├── steering_api.py
│   │   ├── thruster_api.py
│   │   ├── user_api.py
│   │   └── weapon_api.py
│   ├── authentication
│   │   ├── __init__.py
│   │   └── login.py
│   ├── crud
│   │   ├── base_crud.py
│   │   ├── drone_crud.py
│   │   ├── gimbal_crud.py
│   │   ├── __init__.py
│   │   ├── item_crud.py
│   │   ├── price_crud.py
│   │   ├── round_type_crud.py
│   │   ├── scanner_crud.py
│   │   ├── squadron_crud.py
│   │   ├── status_crud.py
│   │   ├── steering_crud.py
│   │   ├── thruster_crud.py
│   │   ├── user_crud.py
│   │   └── weapon_crud.py
│   ├── database
│   │   ├── database.py
│   │   ├── drones.db
│   │   ├── drones_test.db
│   │   └── __init__.py
│   ├── enums
│   │   ├── __init__.py
│   │   ├── round_type.py
│   │   └── status.py
│   ├── error
│   │   ├── error.py
│   │   └── __init__.py
│   ├── fixtures
│   │   ├── gimbal_fixtures.py
│   │   ├── __init__.py
│   │   ├── round_type_fixtures.py
│   │   ├── scanner_fixtures.py
│   │   ├── status_fixtures.py
│   │   ├── steering_fixtures.py
│   │   ├── thruster_fixtures.py
│   │   ├── user_fixtures.py
│   │   └── weapon_fixtures.py
│   ├── model
│   │   ├── base_model.py
│   │   ├── drone_model.py
│   │   ├── __init__.py
│   │   └── squadron_model.py
│   ├── request
│   │   ├── __init__.py
│   │   └── json_request_handler.py
│   ├── response
│   │   ├── __init__.py
│   │   └── json_response.py
│   ├── service
│   │   └── calculate_cost.py
│   ├── transformer
│   │   ├── __init__.py
│   │   ├── json_transformer.py
│   │   └── transformer.py
│   └── validation
│       ├── abstract
│       │   ├── __init__.py
│       │   └── validation_abstract.py
│       ├── drone_validation.py
│       ├── field.py
│       ├── __init__.py
│       ├── validation_link.py
│       └── validations.py
└── tests
    ├── drones_test.db
    ├── __init__.py
    ├── test_api
    │   ├── conftest.py
    │   ├── __init__.py
    │   ├── test_auth.py
    │   ├── test_drone.py
    │   ├── test_gimbal.py
    │   ├── test_price_list.py
    │   ├── test_round_type.py
    │   ├── test_scanner.py
    │   ├── test_squadron.py
    │   ├── test_steering.py
    │   ├── test_thruster.py
    │   └── test_weapon.py
    └── test_crud
        ├── conftest.py
        ├── __init__.py
        ├── test_drone_crud.py
        ├── test_gimbal_crud.py
        ├── test_scanner_crud.py
        ├── test_squadron_crud.py
        ├── test_status_crud.py
        ├── test_steering_crud.py
        ├── test_thruster_crud.py
        ├── test_user_crud.py
        └── test_weapon_crud.py

最佳答案

在 Python 中,导入可以是相对的或绝对的。

绝对导入从项目的根目录解析:

import drone_squadron.api.drone_api

相对导入是从当前的 python 包解析的。

import ..api.drone

在您的情况下,问题不是相对/绝对混淆,PyCharm 总是添加绝对导入。

问题是 PyCharm 可能将项目中的文件夹 drone_squadron 视为“根目录”。错了!根目录是整个git项目对应的顶层文件夹(包含LICENSE.md、README.md等的文件夹)

在 PyCharm 中,右键单击文件夹 drone_squadron,然后打开子菜单 Mark directory as(位于底部),然后选择 Unmark as源根

PyCharm animated screenshot: change root directory

在该操作之后,您的导入将按照您想要的方式添加。

关于python - 无论如何强制 PyCharm 总是使用绝对导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949551/

相关文章:

python - 对 Pandas 数据框中的行对执行函数

python - python的sorted()函数是否保证稳定?

pycharm - 我们应该忽略 Pycharm .idea/folder 中的 Misc.xml 和 <projectFolderName>.iml 吗?

python - 无法在 Django 中显示静态图像和加载 CSS

python - 在 Windows 10 上设置 OpenAI Gym

python - 我安装了 Pandas ,但它不工作

Python:读取文件以将数据存储在数组中的有效方法

python - cross_validation模块是否从sklearn中删除了?如果是,从哪个版本开始?

python - 在 PyCharm 中使用 VPython

pycharm - PyCharm 1.x/2.x 是否提供调试控制台?