Function to import file. py?

Asked

Viewed 60 times

0

Good afternoon, I created a file called test.py with code that fetches information from a database and turns it into a Dataframe. I’m having trouble creating a function and import this file, follow what I tried to do:

import teste.py
teste.ipynb.path.insert('teste.py', "C:\Users\pasta\Documents\WPy64-3760\notebooks")

Modulenotfounderror: No module named 'test.py'; 'test' is not a package.

1 answer

1

This error is being generated because you are trying to import the module py package teste, or at least, that’s how the interpreter understands.

In Python language you should not put the file extension when importing the module. So your code should be like this:

import teste
teste.ipynb.path.insert('teste.py', "C:\Users\pasta\Documents\WPy64-3760\notebooks")

In addition, the point . is used to inform Python that an X module is within a certain package. See the example below:

# Árvore de diretórios:
#
# carro/
# carro/config/
# carro/config/motor.py
# carro/roda.py
# carro/volante.py
# piloto.py

import carro.config.motor # Importa o módulo "motor.py" do pacote "carro/config"
import carro.roda         # Importa o módulo "roda.py" do pacote carro
import carro.volante      # Importa o módulo "volante.py" do pacote carro
import piloto             # Importa o módulo "piloto.py" que está no diretório atual

To learn more about Python import see this another answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.