如何在 VS Code 中正确导入 Python 模块? [英] How to correctly import a Python module in VS Code?

查看:210
本文介绍了如何在 VS Code 中正确导入 Python 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始用 Python 编程,我决定用 Python 编写一些 Delphi 函数.我决定创建一个单独的 Python 模块来保存我的函数.

现在,我尝试导入它,但在 Visual Studio Code 中出现此错误:

无法导入函数"pylint(导入错误)[5, 1]

这是我的代码:

导入系统sys.path.append('/Users/user123/Desktop/Python/Functions/')导入函数

这是一张图片:

解决方案

鉴于您的文件/文件夹结构:

├── 函数│ └── 函数.py├── <主应用文件夹>│ └── app.py

尽管在您将 path/to/Functions 添加到 sys.path 后,您的导入可能会正确运行,但 Pylint 会向您发出警告,因为这不是推荐的声明导入的方式,尤其是当您在应用包/文件夹外部导入模块时.

来自 PEP8 导入样式指南:><块引用>

推荐使用绝对导入,因为它们通常更具可读性并且往往表现得更好(或至少给出更好的错误信息)如果导入系统配置不正确(例如当包内的目录最终位于 sys.path):

import mypkg.sibling从 mypkg 导入兄弟从 mypkg.sibling 导入示例

推荐的解决方案是Functions设置为一个包,在它下面添加一个__init__.py文件:

├──父│ └── 功能│ ├── __init__.py│ └── 函数.py

然后像其中之一一样导入您的函数:

sys.path.append("/path/to/parent")# 选项1从函数导入函数函数.copy()函数.delete()#选项2from Functions.functions 导入复制、删除复制()删除()

两个选项都应该正确运行并满足 PyLint.

现在,如果你真的想做一个像 from functions import func 这样的非绝对导入,并让 PyLint 接受它,我建议将 functions.py 重命名为别的东西.这是因为,在某些不区分大小写的系统上,导入 Functionsfunctions 可能会被视为同一个模块.当您告诉 PyLint 查看 /path/to/Functions(我稍后会展示)时,它可能无法区分 copydeleteFunctionsfunctions 的一部分,它可能仍会显示导入错误.

所以,你需要做的是重命名functions.py(例如filefuncs.py):

├── 函数│ └── filefuncs.py├── <主应用文件夹>│ └── app.py

然后在您的 VS Code 工作区中,将其添加到您的 .vscode/settings.json 文件中,以告诉 PyLint 在哪里查找 filefuncs 模块:

python.linting.pylintArgs":[--init-hook","导入系统;sys.path.append('/path/to/Functions')";]

然后,您现在可以像原始代码一样导入它,但不会出现 PyLint 错误:

sys.path.append("/path/to/Functions")from filefuncs 导入复制,删除复制()删除()

第二种方法可以满足您的需求,但它包含一些 PyLint 工作的变通方法.如果您可以使用我在开始时解释的推荐方式,请改用该方式.

I've recently started programming in Python and I've decided to code some Delphi functions in Python. I've decided to create a single Python module that holds my functions.

Now, I tried to import it, but I get this error in Visual Studio Code:

unable to import 'functions' pylint(import error) [5, 1]

Here is my code:

import sys

sys.path.append('/Users/user123/Desktop/Python/Functions/')

import functions

Here is an image:

解决方案

Given your file/folder structure:

├── Functions
│   └── functions.py
├── <main app folder>
│   └── app.py

Although your imports may run correctly once you've added path/to/Functions to sys.path, Pylint is giving you that warning because that is not the recommended way of declaring imports, especially when you're importing modules outside the app package/folder.

From the PEP8 Style Guide for Imports:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path):

import mypkg.sibling 
from mypkg import sibling 
from mypkg.sibling import example

The recommended solution is to setup Functions as a package by adding a __init__.py file under it:

├── parent
│   └── Functions
│       ├── __init__.py
│       └── functions.py

then importing your functions like one of these:

sys.path.append("/path/to/parent")

# option 1
from Functions import functions
functions.copy()
functions.delete()

# option2
from Functions.functions import copy, delete
copy()
delete()

Both options should run correctly and satisfy PyLint.

Now, if you really want to do a non-absolute import like from functions import func, and get PyLint to accept that, I recommend renaming functions.py to something else. This is because, on some case-insensitive systems, importing Functions and functions could get treated as the same module. When you tell PyLint to look into /path/to/Functions (I'll show later), it might not be able to differentiate if copy and delete is part of Functions or of functions, and it might still show an import-error.

So, what you need to do is rename functions.py (ex. filefuncs.py):

├── Functions
│   └── filefuncs.py
├── <main app folder>
│   └── app.py

Then in you VS Code workspace, add this to your .vscode/settings.json file to tell PyLint where to look for the filefuncs module:

"python.linting.pylintArgs": [
    "--init-hook",
    "import sys; sys.path.append('/path/to/Functions')"
]

Then you can now import it same as your original code but without PyLint errors:

sys.path.append("/path/to/Functions")
from filefuncs import copy, delete
copy()
delete()

The second way will get you what you need, but it contains some workarounds for PyLint to work. If you can use the recommended way I explained at the start, use that instead.

这篇关于如何在 VS Code 中正确导入 Python 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆