huy
huy

Reputation: 1914

How to run python script on Google Colab without changing directory

I have a working directory on Colab like this:

content/
|---work/
   |--- main.py
   |--- utils/
        |--- tool.py

In file main.py, I import file tool.py:

from utils import tool

When run a script on Colab, I use this command:

%%bash
cd work
python main.py

But in this way, I cannot see any output when the script is running, it has to stop to able to see it.

If I use this command, I can see the output:

!python /content/work/main.py

But there will be an error about No such file or directory if I import the tool.py file.

Do you guys know how to run the main.py script and can see the output during the execution?

Upvotes: 1

Views: 1626

Answers (1)

Jimit Vaghela
Jimit Vaghela

Reputation: 758

You are trying to import from a different module which is present as a subset. Why not put the tool.py into the same directory as well? or else I'd suggest:

%cd "/content/work/" and execute !python "main.py" that contains from utils import tool

Upvotes: 1

Related Questions