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.
Related
I am trying to publish a package on PyPI. First, I uploaded it to TestPyPI then installed it on a fresh Conda environment with
python3 -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ neuralpp
However I am unable to import some classes from the package. The minimal example below illustrates this:
# main.py
from neuralpp.inference.graphical_model.representation.table.pytorch_log_table import PyTorchLogTable
from neuralpp.inference.graphical_model.representation.factor import PyTorchTableFactor
When I run python main.py, the first import (PyTorchLogTable) works, but not the second (PyTorchTableFactor). See the error message at the end of the question.
I checked the package files (within the Conda environment) and the required files are present. This also works fine in the original project I uploaded.
Because the error message mentions __init__.py, I've added __all__ = ['PyTorchTableFactor'] to it, even though I know the use of __all__ is not mandatory. However, this has not changed anything. Besides, the __init__.py for PyTorchLogTable is also empty and that is imported correctly, further confirming that this is not the issue.
What is wrong here, and how can I fix it?
Error message:
File "main.py", line 2, in <module>
from neuralpp.inference.graphical_model.representation.factor import PyTorchTableFactor
ImportError: cannot import name 'PyTorchTableFactor' from 'neuralpp.inference.graphical_model.representation.factor' (/Users/rodrigobraz/opt/anaconda3/envs/test/lib/python3.8/site-packages/neuralpp/inference/graphical_model/representation/factor/__init__.py)
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)
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
I have a Colab notebook open and have my files cloned from a github repo. I want to run a python script called models.py. In this file, I am using pandas. When I run this line in Colab:
!python3 models.py
I get the following error:
Traceback (most recent call last):
File "models.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
However, if I run in a cell on Google Colab:
!pip3 list
I find that pandas is indeed installed:
pandas 0.25.3
My assumption is that when I run the script, it is not able to see the libraries I have installed but I am unsure how to fix this issue.
If I run:
!which python3
I get:
/usr/local/bin/python3
The python file I am trying to run is under:
/content/my_project/models.py
Should I instead take a different approach to running this file?
Instead of
!python3 models.py
You can use
%run models.py
In a clean Colab runtime, the location of python 3 is different than what you show in your question:
!which python3
# /usr/bin/python3
It looks like you are installing another Python instance in your VM. To ensure you're using Colab's bundled Python 3 executable, try using
!/usr/bin/python3 models.py
If you actually want to use the new python instance you've installed, you'll have to also install whatever packages you need. For example
!/usr/local/bin/python3 -m pip install pandas
I have a requirement to import a dependency using it's source directory. (Names are obfuscated because this is for work).
So I used conda develop which adds a conda.pth file in site-packages
[user#user folder]$ conda develop /path/to/source/
added /path/to/source/
completed operation for: /path/to/source/
The new module resolves, when I run the code using python.py, but then it doesn't resolve dependencies in conda itself. ie:
(dq) [user#user]$ python file.py
Traceback (most recent call last):
File "file.py", line 10, in <module>
import utils as utils
*...
Various stack trace with import getting resolved
...*
import Pyro.errors
ImportError: No module named errors
So Pyro is a package installed in the dq conda environment, but for some reason through the source code imported through conda develop, it can't find the import. I'm not sure if this itself is an issue, but the developer of the code also had the ingenious idea of naming the module Pyro.py and then importing Pyro.errors at the top of the module. Is there a way to have conda imports take precedence over the source code? Or be resolved in the first place?
Thanks in advance for the help!
Probably you should change the package of Python you work with to another newer or older one from the Conda page shown by Jupyter as shown in this .