ModuleNotFoundError in Colaboratory - python

I am trying to run my project using the gpus but I can't get it to work.
I have ran the following commands:
from google.colab import drive
drive.mount('/content/gdrive')
%cd gdrive/MyDrive/project_folder
import sys
sys.path.append('/content/gdrive/MyDrive/project_folder')
I then try to run my main script from project_folder by using
! python property_prediction/predict.py
In the first line of predict.py I import a module from the folder 'project_folder' but that gives this error in colab:
File "property_prediction/predict.py", line 17, in <module>
from GP.kernels import Shortest_Path
ModuleNotFoundError: No module named 'GP
Why is it not finding the folder GP which contains my kernels script?

Try to replace your running command with:
!python -m property_prediction.predict
Or better:
from property_prediction.predict import predict # or whatever your main function is called
predict()
NB: This is of course assuming that you have a module named GP in the folder project_folder
If none of this work, you might be interested in reading this or other articles about imports with python (this is most likely not a problem of google collab)

Related

No module named 'utils.utils' in Google Colab

I'm trying to run this python code in Google Colab and I always get this error that the utils module is not installed or does not exist
yet I've ran !pip install utils and the still the same issue.
I've tried running it on my computer and it works without issues but I can't actually run it due to limited resources of my pc.
anyway anyone has a solution for this ?
Traceback (most recent call last):
File "/content/GNN-for-text-classification/preprocess/build_graph.py", line 15, in <module>
from utils.utils import loadWord2Vec, clean_str
ModuleNotFoundError: No module named 'utils.utils'
I am assuming you are using this GNN-for-Text-Classification.
Now, you have probably cloned the repository in your local system and you're running the .py files from there.
But, a New Notebook in Colab will not have the files that you have cloned/downloaded. So, when you're doing
!pip install utils
The utils package from Pypi is getting installed which doesn't contain the functions you require.
What you need is actually the utils module from GNN-for-Text-Classification, for which you'll need to clone and cd into the folder in Colab itself. Just run the following in your Colab Notebook:
!git clone https://github.com/zshicode/GNN-for-text-classification.git
%cd GNN-for-text-classification/
%ls
This will clone the repo, cd into the folder and view the contents where you will be able to find the utils module that you need.
Now you can import stuff like loadWord2Vec, clean_str without any errors.
Note that this cloning is not permanent since a new Colab instance will not keep the changes from the old one.

python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.

Python imports from the same folder doesn't work

I created a working folder: C:\Users\robin\bayesian-optim
Inside there are module1_test.py and work.py
I have some functions in module1_test.py that I want to use in work.py, but using import module1_test as mmm in the shell gives the following error :
ImportError: No module named 'dossier_module'
It is the first thing that I do not understand. To go further,
I tried this :
import sys
sys.path.insert(0,'C:/Users/robin/bayesian-optim')
Then it worked, but my real goal is to clone some git codes in this folder and to call them instead of my example module1_test.py. When I do so, cloning "pymoo" in C:\Users\robin\bayesian-optim, I have the same problem :
File "C:/Users/robin/bayesian-optim\pymoo\pymoo\__init__.py", line 1, in <module>
from pymoo.version import __version__
ImportError: No module named 'pymoo.version'
All the .py folders inside of the cloned repo are not foundable, and it would mean that I would have to add at the beginning of every code "import sys, sys.path.insert(0, path/to/this/folder)". Can somebody help me ?

Google Colab : ModuleNotFoundError: No module named 'base_positioner'

I am working on a python program in colab.
I need to import another file here. The file is saved by the name "base_positioner.ipynb" in google drive....
I have gone through multiple resources to see how to do this import and I have done the following:
from google.colab import drive
drive.mount('/content/gdrive')
%cd /content/gdrive/My Drive
On running !ls , I see 'base_positioner.ipynb' in the list but
still running : import base_positioner throws the module not found error
I had also tried the following but with no success in importing the desired file:
sys.path.append('/content/gdrive/My Drive/Colab Notebooks')
What else should I try??
This could happen if you haven't mounted your Drive on Colab to the backend properly and also possibly if your file layout in Drive is distinct from the file layout in Colab. Are you running the import command without running the following code?
from google.colab import drive
drive.mount('/content/gdrive')
%cd /content/gdrive/My Drive
If you are doing that then this won't work, as this is a pre-requisite for the mounting to take place (i.e. not running the cells sequentially). You can also try restarting Google Colab and this often fixes any strange errors.
Update:
As you mentioned, the import error likely happens due to its configuration in the main file (i.e. it requires the file to be in the .py format to be imported just as import base_positioner).
To import .ipynb extension file you will need to follow the following process:
If you want to import A.ipynb in B.ipynb write
import import_ipynb
import A
The import_ipynb module can be installed via pip or any other relevant ways.
pip install import_ipynb

ModuleNotFoundError: No module named 'data' in GoogleColab

I had import and mount the data in ggdrive by this code
import pandas as pd
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
And then i call back a def in this file
from data.generator import DataGenerator
So i had a problem with this module name,it isn't name of a module ,it's a name of a file in direct folder. Hope someone can solve this problem for me
You have to run the python script from the right folder. So before running the python script add a line like:
%cd "/content/gdrive/MyDrive/Machine Learning/005 Handwriting Recognition/handwritten-text-recognition/src"

Categories

Resources