Colab ModuleNotFoundError - google-colaboratory

I have been following
https://colab.research.google.com/drive/12qC2abKAIAlUM_jNAokGlooKY-idbSxi
pretty exactly but I still cannot access any of my code from other files
My attempt:
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/*.ipynb
!cat '/content/gdrive/My Drive/Colab Notebooks/module_i_want.ipynb'
import sys
sys.path.append('/content/gdrive/My Drive')
import module_i_want as mod
mod.loadData()
I am getting:
ModuleNotFoundError: No module named 'module_i_want'
I have tried:
import os
print(os.path.dirname(os.path.realpath('module_i_want.ipynb')))
print(os.path.dirname(os.path.realpath('module_im_in.ipynb')))
and they both return
/content
I can see both files to the left in my mounted drive (in the same folder) but I can't seem to find any way to import anything

Resolved:
For .ipynb files in Google Colab you need to specifically install
!pip install import_ipynb
and then:
import import_ipynb
in order to deal with this type of file

Related

ModuleNotFoundError: No module named 'pandas._libs.interval' | Installed pandas from git in Docker container

This is not a duplicate of existing questions because:
I'm contributing to the pandas repository itself.
I've installed pandas using the git repo and not pip.
I've used a Docker container as suggested by pandas to create the development environment.
The pandas installation is successful & any file is not missing. I've manually verified that pandas._libs.interval is present.
When I tried to import from pandas, I'd get this error:
ImportError while loading conftest '/workspaces/pandas/pandas/conftest.py'.
../../../__init__.py:22: in <module>
from pandas.compat import is_numpy_dev as _is_numpy_dev
../../../compat/__init__.py:15: in <module>
from pandas.compat.numpy import (
../../../compat/numpy/__init__.py:7: in <module>
from pandas.util.version import Version
../../../util/__init__.py:1: in <module>
from pandas.util._decorators import ( # noqa
../../../util/_decorators.py:14: in <module>
from pandas._libs.properties import cache_readonly # noqa
../../../_libs/__init__.py:13: in <module>
from pandas._libs.interval import Interval
E ModuleNotFoundError: No module named 'pandas._libs.interval'
The solution is to rebuild the c extensions.
python setup.py clean (optional, use if 2. doesn't work)
python setup.py build_ext -j 4
Credits: #MarcoGorelli from the pandas community on Gitter.
More on why this solution works:
I suspect that while docker was building the remote container, there were some issues due to an unreliable internet connection.
As all modules were indeed present, one of the only possibilities would be that they couldn't be accessed by Python. The most plausible reason is an issue with the C compiler, something related to cython (interval is a.pyx file).
Also see: https://pandas.pydata.org/docs/development/contributing_environment.html#creating-a-python-environment

Error while importing a custom module in colab

I'm trying to import a custom module to my script but colab won't find the module from my drive.
I'll add a screenshot so you can see the directory:
Screenshot of directory
this is the code with which I am trying to import the module:
import numpy as np
import matplotlib.pyplot as plt
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"
import black_sholes
When I try to run this I get the following Error message:
ModuleNotFoundError: No module named 'black_sholes'
Thanks in advance for your help!
You can add your gdrive to PYTHONPATH:
import sys
sys.path.insert(0, "/content/gdrive/My Drive/Colab Notebooks/Heston")
import black_scholes
If you don't want to modify the sys.path you can just run your module, but then you will loose black_scholes namespace:
%run "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"
You forgot the dot at the end of
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"
It should be
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py" .

ModuleNotFoundError after pip install -e

I have the setup.py code as below:
from setuptools import setups, PEP420PackageFinder
setup(
name='abc'
,packages=PEP420PackageFinder.find(include=['abc.*'],
exclude=['abc.score_pipeline.x'])
)
And the file path is as below:
/current/
/current/setup.py
/current/abc/score_pipeline/x
/current/abc/score_pipeline/y
/current/abc/score_pipeline/y/lalala
Under /current/ I ran pip install -e, which has no problem.
Then in script I'd like to from abc.score_pipeline.y import lalala, but it complains No Module named abc.score_pipeline; I think I could import abc, but not abc.score_pipeline.
Why can't I import abc.score_pipeline? Really confused by this and driven crazy by such seemingly simple relative path issue.
Thanks!

How to install and use IB API within Colab?

I am creating a notebook on Google Colab to run trading algorithms which execute on Interactive Brokers (IB). I want to use the IB API for this.
My current code downloads and installs the API:
# Install IB and related libraries
!wget -cq http://interactivebrokers.github.io/downloads/twsapi_macunix.975.01.zip
!unzip -qq -o twsapi_macunix.975.01.zip
!cd IBJts/source/pythonclient && python setup.py build install
!pip install ib_insync
!pip install ibapi
# Import generic libraries
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
import pandas as pd
import numpy as np
import matplotlib as plt
import time
from datetime import datetime
import argparse
# Import IB and IB_InSync, and start an IB connection
from ib_insync import *
from ibapi import *
The last line returns an error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-3cdef93fab15> in <module>()
48 from ib_insync import *
---> 49 from ibapi import *
ModuleNotFoundError: No module named 'ibapi'
As the installation seems to run properly, I do not understand why I cannot import the API for use in the subsequent code.
Thanks already for your help!
Found a workaround by installing the library directly from the .egg file:
'''
ib_path='/usr/local/lib/python3.6/dist-packages/ibapi-9.75.1-py3.6.egg'
sys.path.append(ib_path)
from ibapi import *
'''

tensorflow object detection API ImportError when generating PASCAL TFRecord files

I'm trying to use the Tensorflow Object Detection API and I've successfully tested the installation,but we I try to generate the PASCAL VOC TFRecord files with the given command
python object_detection/create_pascal_tf_record.py \
--label_map_path=object_detection/data/pascal_label_map.pbtxt \
--data_dir=VOCdevkit --year=VOC2012 --set=train \
--output_path=pascal_train.record
I encountered the following error:
Traceback (most recent call last):
File "object_detection/create_pascal_tf_record.py", line 36, in <module>
from object_detection.utils import dataset_util
ImportError: No module named object_detection.utils
my PYTHONPATH is:
:/usr/local/lib/python2.7/dist-packages/tensorflow/models:/usr/local/lib/python2.7/dist-packages/tensorflow/models/slim
and I'm running the above command in the /models directory,anyone who knows how to fix this problem?
I had the same problem and I solved it by adding :
import os
import sys
sys.path.append(os.path.abspath("./object_detection"))
and
from object_detection.utils import dataset_util
becomes
from utils import dataset_util
It's better for you to add 'object_detection' to the env path, not in the python script.
You can do this in the shell, at the 'object_detection' directory.
export PYTHONPATH=$PYTHONPATH:`pwd`
Or add the path to .bashrc/.zshrc (depend on your shell), so don‘t need to export every time.
echo "export PYTHONPATH=$PYTHONPATH:`pwd` >> ~/.bashrc