Mahdi Attar
Mahdi Attar

Reputation: 43

How to install a predefined package(class) in google colab(python,Jupyter notebook)?

I have already defined a class which I want to use in my code. in order to import it I know I have to install it first.(I am using google's colab)

to install first I uploaded the .py file into my drive which is mounted and no problem with that.

but still there is problem with installing the package.

how can I use this predefined class correctly in my code?

    !pip install robotdef.py
Collecting robotdef.py
  ERROR: Could not find a version that satisfies the requirement robotdef.py (from versions: none)
ERROR: No matching distribution found for robotdef.py

Upvotes: 0

Views: 443

Answers (1)

yeiichi
yeiichi

Reputation: 178

I think I overlooked one thing.

If "I uploaded the .py file into my drive which is mounted and no problem with that." is the situation, you don't have to run pip, but you'll need to expand the Python path to the directory where your package(class) is installed.

I hope this may help.

import sys

sys.path.append('/foo/bar/your-modules-path')

pip fetches a package from the Python Package Index (PyPI) repository. This means that the package must be registered on the repository first.

I tried to find 'robotdef.py' or 'robotdef' using a search form on the PyPI website, but it seems that 'robotdef.py' hasn't been registered yet...

If it's on the repository, !pip install <package name> will work fine on the Colab.

Supplement

Sorry, my answer was incomplete.

It's not necessary a package to be registered on the PyPI repository, but you can install a package from local archives, etc.

Upvotes: 1

Related Questions