简体   繁体   English

我如何在python中正确导入一个包?

[英]how do i correctly import a package in python?

在 vscode 中导入包时出错

Hello everyone im currently learning python and i im having some problems importing modules and packages.大家好,我目前正在学习 python,我在导入模块和包时遇到了一些问题。 Actually i think is more of a problem with vscode.实际上,我认为 vscode 的问题更多。

i have this package called "paquete" with a module (funciones) that i want to import to my "main" with some fuctions in it to test if it all works correctly but i still getting "emphasized items and unresolved-import" warnings.我有一个名为“paquete”的包,其中包含一个模块(funciones),我想将其导入到我的“main”中,其中包含一些功能以测试它是否都能正常工作,但我仍然收到“强调项目和未解决的导入”警告。

but for some reason it works just fine.但由于某种原因,它工作得很好。

is more of a annoying thing.更烦人的事情。

EDIT:编辑:

在此处输入图片说明 module with the function "funcion"具有“功能”功能的模块

在此处输入图片说明

the warning that appears in the main folder "prueba" is "emphasized items" i tried what u guys told me to do but it stills shows the warnings出现在主文件夹“prueba”中的警告是“强调项目”我尝试了你们让我做的事情,但它仍然显示警告

As you are trying to import a specific function from module in python You should use in this manner:当您尝试从 python 中的模块导入特定函数时,您应该以这种方式使用:

from paquete import funciones

If you want to import full module then use:如果要导入完整模块,请使用:

import paquete

I can't tell whats in the funciones file.我不知道 funciones 文件中有什么。 But normally this yellow import lines are telling you that you import functions, which you dont use.但是通常这个黄色的导入行告诉你你导入了你不使用的函数。

Try this instead if you only want如果你只想试试这个

funcion功能

to be imported.要进口。

from paquete.funcions import funcion

This is also better because you import only the functions you need, not all of the functions you declared in the other file.这也更好,因为您只导入您需要的函数,而不是您在另一个文件中声明的所有函数。 Also all imports of the other file will be loaded into your file if you import with an asterix.如果您使用星号导入,其他文件的所有导入也将加载到您的文件中。

The issue is you are doing all of this from within a directory named prueba .问题是您在名为prueba的目录中执行所有这些prueba If you changed the import to from prueba.paquete.funciones import * it should work after you add a __init__.py file to your prueba directory.如果您将导入更改为from prueba.paquete.funciones import *它应该在您将__init__.py文件添加到您的prueba目录后工作。 The other option is to use a relative import: from .paquete.funciones import * .另一种选择是使用相对导入: from .paquete.funciones import *

But do note that using import * is strongly discouraged when you are not working within the REPL.但请注意,当您不在 REPL 中工作时,强烈建议不要使用import * It's much better to import to the module and then reference things off the module, eg from prueba.paquete import funciones , from .paquete import funciones , or import prueba.paquete.funciones .最好先导入模块,然后从模块中引用内容,例如from prueba.paquete import funcionesfrom .paquete import funcionesimport prueba.paquete.funciones That way you know exactly where things in your code came from without having to read the top of your file.这样您就可以确切地知道代码中的内容来自哪里,而无需阅读文件的顶部。

pip3 intall "name" pip3 安装“名称”

Use Pycharm, rather than Vscode使用 Pycharm,而不是 Vscode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM