Vasil Kosturski
Vasil Kosturski

Reputation: 159

How to reference custom Python files when training a model with tensorflow_cloud in GCP?

I'm trying to train a Tensorflow model in Google Cloud using tensorflow_cloud.

So I have the following code triggering the training:

import tensorflow_cloud as tfc
tfc.run(
    requirements_txt="requirements.txt",
    distribution_strategy="auto",
    docker_image_bucket_name=<bucket-name>
) 

I have a simple python file with some util functions let's say its' name is utils.py

So, in the notebook I'm importing this file like so:

from utils import *

My question is - How do I reference this file when running the notebook in GCP?

At the moment I get a module not found error indicating the utils file can't be found.

I'm trying to copy the file to my GCP bucket but it still says it can't find it.

Upvotes: 2

Views: 91

Answers (1)

Alexandre Moraes
Alexandre Moraes

Reputation: 4051

I understand you want to use import to import and access functions, which you have in a separate .py file. The error you received happened because the interpreter could not find your package (.py) in the current or gobal directories. For this reason, you need to add the proper path and import the module.

In order to access them without an error, you need to follow the steps below,

  1. Create a folder within your Jupyter notebook by clicking in the New Folder button;
  2. Upload the .py file there;
  3. You need to add path to the file which is located in the new directory you created in the first step. Now, in the .ipynb you want to import the file, execute
    import sys  
    sys.path.insert(0,"your_path/package")
    import your_file as pck

Note: replace the proper fields with your file path and file name

It will be imported successfully. Then you will be able to use the functions within the file.

Upvotes: 1

Related Questions