Python imported functions are not being executed - missing libraries

57 views Asked by At

I am finalizing a project where I need to split the requirements.txt , helper_functions.py and , libraries.py -libraries that are used within the functions- into (3) separate files for code beautification .

I created the 3 files, uploaded them on my cloud and made them accessible to my google Colab notebook:

  1. I downloaded the requirements.txt and installed the packages every time I run the jupyter notebook using the command !pip install -r requirements.txt

  2. I downloaded the helper_functions and imported them into my environment using the command from helper_functions import * where I can see them.

  3. This is the problem now, I downloaded the libraries.py and tried to import them using using the command from libraries import * or %run -i libraries.py . The problem I have is when I try to call any of the functions that require one of the libraries e.g boto3 library or the shortcut of np (though both of them exist in my libraries.py file), I get an error of

    name 'boto3' is not defined
    name 'np' is not defined
    

Here are the snapshots of my files that are loaded into my Colab environment

`requrinments.txt`

boto3

numpy

`libraries.py file`

import boto3

import numpy as np

`helper_functions.py file`

def load_data(input):

data = np.(…..)

what should I do ?

2

There are 2 answers

0
TimothyH On

Python modules are imported into a given namespace. To use a module of a module you can access it by module.submodule. However this is terrible to do and you should just about always import them directly into your module since almost all of the time they will be identical.

import mymod
np = mymod.numpy

The above should be akin to the below for all non-hacky code.

import numpy as np
0
Wael On

The libraries related to the helper functions must exist in the same file, since libraries.py will import the libraries to a name space that helper functions file doesn't access it