简体   繁体   中英

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. Actually i think is more of a problem with 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.

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

As you are trying to import a specific function from module in python You should use in this manner:

from paquete import funciones

If you want to import full module then use:

import paquete

I can't tell whats in the funciones file. 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 . 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. The other option is to use a relative import: from .paquete.funciones import * .

But do note that using import * is strongly discouraged when you are not working within the REPL. 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 . That way you know exactly where things in your code came from without having to read the top of your file.

pip3 intall "name"

Use Pycharm, rather than Vscode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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