Home > Software engineering >  How to import dataframe from .py file to .ipynb file?
How to import dataframe from .py file to .ipynb file?

Time:12-13

I am trying to import a dataframe from funcs.py file to data.ipynb file, both are in the same directory

funcs.py

df_course = pd.read_sql('SELECT COURSEID, COURSENAME FROM COURSE', con=conn)

here conn is the pyodbc connection , it is working fine as i am able to view dataframe in funcs.py

error

When i tried to import df_course in data.ipynb it is giving me error, i am doing like this

import funcs.df_course as df_course

but it is giving me error that cannot import df_course from funcs

Please help me in this problem

CodePudding user response:

Simply use:

from funcs import df_course

because funcs is not a package, a directory that contains an __init__.py file.

CodePudding user response:

Try this:

import sys
function_path = r'C:\Users\hogsione\jupyter' # Path of funcs.py file
sys.path.insert(1, function_path)

# Now import df_course
from funcs import df_course

CodePudding user response:

Instead Of import funcs.df_course as df_course;

Use This:- from funcs import df_course as df_course

For The Name That You Want, as df_course is not necessary, but if u want to import it as df or something, then you can use df_course..

Best Of Luck!

  • Related