Importing Colab modules into Colab - python-3.x

I am new to using Colab, I want to create modules that are imported into other notebooks. I have tried a number of methods and the best I end up with is this error message:
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import converters
ModuleNotFoundError: No module named 'converters'
Can you import a *.ipynb as a module into Colab?
I have mounted the gdrive and set the root dir - see code below.
from google.colab import drive
drive.mount('/content/gdrive/', force_remount=True)
root_dir = "content/gdrive/My Drive/Colab Notebooks/projects/utilities/"
base_dir = root_dir + "projects/"
I have tried other methods as well, I have also tried sharing the notebook. help. thanks

Try :
# (1) Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/drive')
# (2) Insert the directory using sys
import sys
sys.path.insert(0,’/content/gdrive/My Drive/Colab Notebooks/projects/utilities/’)
# Import your module or file
import my_module
OR
from google.colab import drive
drive.mount('/content/drive')
sys.path.append('/content/gdrive/My Drive/Colab Notebooks/projects/utilities/')
!cp -r "/content/drive/My Drive/Colab Notebooks/projects/utilities/my_module.ipynb" '/content/'

Related

Getting 'UnidentifiedImageError: cannot identify image file error' while converting pdf to image on google colab

I am using pdf2image to convert a pdf file into image. I am using the method convert_from_path. However, I keep getting the above-mentioned error on google colab. Surprisingly this does not happen when I execute the same code on Jupyter notebook on my local machine.I need to use colab. How do I solve this error?
import pytesseract
import shutil
import os
import random
try:
from PIL import Image
except ImportError:
import Image
import cv2
from pdf2image import convert_from_path
from google.colab import files
uploaded = files.upload()
pages=convert_from_path(r"foo.pdf",500)
After executing the following code, I get the error :
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fe6abfae5f0>
I have installed poppler-utils

Not able to load python custom module from another directory

I want to load a custom python module from another directory. This question also has been asked many times and I have followed lot of links, and I have a solution. However, it is not working for me. I am fairly new to Python and looks like I am making a simple mistake which I am not able to find.
Below is the hierarchy. I want to import 'extollo_keyvault.py' inside 'testCode.py'. I can see the path for 'extollo_keyvault.py' when I print 'sys.path', however, the script execution fails saying that unable to find that module
Code:
import os
import sys
path_to_extolloKeyvault_module = os.path.join(os.getcwd(), 'extollo_instance', 'hardened', 'extollo_keyvault.py')
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault
Error:
Traceback (most recent call last):
File "c:/Users/manjug/source/repos/extollo-instance-march-31/extollo_instance/Instance/testCode.py", line 6, in <module>
import extollo_keyvault
ModuleNotFoundError: No module named 'extollo_keyvault'
Your current code includes /Instance.
Try this code:
import os
import sys
from pathlib import Path
path = Path(os.getcwd())
path_to_extolloKeyvault_module = os.path.join(path.parent.absolute(), 'extollo-instance-march-31', 'extollo_instance', 'hardened'
sys.path.insert(0, path_to_extolloKeyvault_module)
import extollo_keyvault

How to import .py file in jupyter notebook from AWS S3

I am working on a jupyter notebook in AWS, I have two files: main.ipynb and utils.py, what I would like to do is to import utils in my jupyter notebook file.
Unfortunately I have tried the following solutions and none of them are working:
import sys
sys.path.append("/home/jovyan/dir1")
%load utils.py
And to directly import after changing the directory
import utils
my file "utils.py":
def hello():
print("hello")
Problem solved:
the solution is to add those :
s3 = boto3.resource('s3')
s3.meta.client.download_file(os.environ['S3_BUCKET'], "dir1/utils.py", "utils.py")
import utils
Export python path to the path from where you are trying to import utils.
Run this below command from you file path
export $PYTHONPATH=.

GoogleAuth: ModuleNotFoundError: No module named 'google.colab'

I want to import data from google drive to a data frame. But my code breaks down at the 'from google.colab import auth' step. It says google.colab module not found.
Tried suggestions at
How to resolve: ModuleNotFoundError: No module named 'google.colab'
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
Error message:
ModuleNotFoundError: No module named 'google.colab'
If you are running the code on your local machine, try using PyDrive to read from google drive to your local machine:
!pip install PyDrive
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth)
# Auto-iterate through all files that matches this query
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
# You can download file content using GetContentFile(filename) by initializing
# GoogleDriveFile instance with file id.
file6 = drive.CreateFile({'id': file5['id']})
file6.GetContentFile('catlove.png') # Download file as 'catlove.png'.
That's probably because the package isn't installed. Download and install it with PIP:
pip install google-colab

I can't import modules from other folders

I'm a bit new to the Python world. I'm using Python3 and having hard times with imports.
I was using PyCharm on Windows to write an application. Everything was working through the project until I switched to Linux and VS Code.
Now I can't use absolute imports for importing modules from other packages in the same project.
For example, I'd like to import from module cards all available card types.
I tested the classes and everything is ok. I'm only getting this issue with the import stuff.
The project structure:
/
|-cards
|-__init__.py
|-card.py
|-monster_card.py
|-spell_card.py
|-trap_card.py
|-ritual_card.py
|-deck
|-__init__py
|-deck.py
|-system
# This is the code in __init__.py in cads package
from . trap_card import TrapCard
from . spell_card import SpellCard
from . ritual_card import RitualCard
from . monster_card import MonsterCard
__all__ = [TrapCard, SpellCard, RitualCard, MonsterCard]
# The following line, for example, does not work from inside another package
# I'm trying to import the modules in cards from deck
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
When I try to import packages from another folders I get this error message:
Traceback (most recent call last):
File "/root/git-repos/Yu-Gi-Oh/decks/deck.py", line 3, in
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
ModuleNotFoundError: No module named 'cards'
When you call import *, python search the module from sys.path. You need to add your root dir into sys.path before call import stmt.
For your case, your root dir is /.
like:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from cards import *
other way
Add file __init__.py into your root dir for make it become a module. Then change from cards import * to from .cards import *.

Resources