Search code examples
python-3.xgoogle-colaboratoryfilepath

one path level up using ../ in google colab


I have to work on a GitHub code, code of a paper, which is huge, so it would take a lot time to change it manually.
update question:
the code contains several python files and modules in deferent directories, something like this:

algorithm1/
├─ data/
├─ model/
├─ some_func.py
├─ evaluate.py
├─ train.py
utils/
├─ data/
├─ model/
├─ some_helper.py
├─ plot.py
├─ anything_else.py

and I am trying to train/evaluate my model like this:

%run /content/drive/algorithm1/train.py

end of update

the problem is in some files, it uses ../ to reference to the parent directory (for example: save_dir='../dir_A/dir_B/dir_C)
but colab does not recognize it:

FileNotFoundError: [Errno 2] No such file or directory: '../dir_A/dir_B/dir_C/file.csv'

I know I can fix it by changing it like this:

save_dir='/content/drive/dir_D/dir_A/dir_B/dir_C/'

but it would take too much time, since every time parent directory (i.e. dir_D) would be different. and this statement save_dir='../dir1/dir2 would be different (for example sys.path.append('../dir_E/') or os.path.join('..', 'dir_F') etc.

is there anyway to make colab recognize ../ or edit them faster?


Solution

  • okey I got it. there was three problems in my tries.
    first I was trying to run the python scripts like this:

    %matplotlib inline
    %run my_python_file.py 
    

    now it replaced %run with !python and it worked

    the second problem was that in each file, I must add this to let interpreter search the right place ( in my code, there are some modules in one level up path ):

    import sys
    sys.path.append('../')
    

    third, I must change my directory next to the python file I was trying to run, and I was addressing to the file directly, so I changed :

    %run /content/drive/dir1/dir2/my_python_file.py
    

    to

    os.chdir('/content/drive/dir1/dir2/')
    !python my_python_file.py
    

    I updated the question to reduce misunderstanding in the later references