I'm working with google-colab.
Can it be possible to call a function from one colab file in to another colab project, as we do in python like importing file.
ex.
I have two colab files 1.ipynb and 2.ipynb.
Let's say 1.ipynb has ABC function.
Now I want to utilize this already implemented ABC function into 2.ipynb
is there any way I can do it? or it's not possible?
It's possible using the import-ipynb libary:
!pip install import-ipynb
import import_ipynb
This lets you import code from .ipynb files in the same way you would from .py files - simply by calling import filename or from filename import function, class, etc. (which is more advisable).
Assuming you have your notebooks neatly organized on your Google Drive, eg. in MyDrive/Colab_Notebooks/ you can then mount it:
from google.colab import drive
drive.mount('/content/drive')
and import the functions you want from the source notebook:
from drive.MyDrive.Colab_Notebooks.notebook1 import ABC, someOtherFunction
Related
I have several different Notebooks in Google Colab that I use to automate some tasks. I want to create a single Notebook that runs all this tasks, so I can open a single tab with one single Notebook, run it and it will run all other tasks inside these different Notebooks.
I have two questions regarding this problem:
The solution I found is not working (I will describe it below). How
do I make it work?
Is there a better solution than the one I found?
About the first question:
Image I have Notebook_1 and Notebook_2 each one with a bunch of functions that automate my tasks. What I am doing is, downloading them as Notebook_1.py and Notebook_2.py, saving these files in a Google Drive folder. Then in my Notebook_main, which is the notebook that should house all notebooks, I run:
# Mounts Google Drive
from google.colab import drive
drive.mount('/content/drive')
# Copies .py files into Google Colab
!cp /content/drive/MyDrive/my_modules/Notebook_1.py /content
!cp /content/drive/MyDrive/my_modules/Notebook_2.py /content
# Import Modules
import Notebook_1
import Notebook_2
If I want to run a simple function inside these modules I just do:
Notebook_1.run_simple_function()
and this works. My problem happens when the function I am trying to run from the Notebook_1, for example, uses another module. Then I get the following error:
name 'os' is not defined
I imagine it happens because inside Notebook_1.py I call:
import os
...
os.remove(os.path.join(dir_download, item))
...
And I also think this will happen with all the modules that I call inside Notebook_1.py.
I have tried importing theses modules in Notebook_main, but it did not work. I do not know how to fix this. I need help.
Another issue is that I use a lot of Selenium, which needs to be installed in Google Colab before being imported. So I need to install and import it in Notebook_main and when I run a function with Notebook_1.run_function_that_uses_selenium() it should use the import from Notebook_main.
The second question is simpler. I just want to know if there is a better way to achieve the same result, i.e. run different Notebooks in Google Colab from a single notebook.
My constriction is that I can only use Google Colab and other Google related Platforms, I can not run anything locally.
I have a python app that uses around 15 pip libraries.
It also requires Azure Storage as it creates files.
I'd like to publish this app to Azure Functions. How do I do this?
How does Azure Function manage all the python libraries that I need?
I can't seem to find any sample code.
My code is basically like this:
import libA
import libB
import libC
def function1(...):
Commands-For-Function1
def function2(...):
Commands-For-Function2
def function3(...):
Commands-For-Function3
function1(param1, param2).. # Execution
But with Azure function apps, it looks for an init function. How would I integrate my function into an azure function?
Also, would Azure Container Instances not be a better solution? I'd just have to containerise my solution and publish it.
Thanks
Azure Functions Python Project has some default folder structure recommended by Microsoft:
Whatever the dependencies, libraries and the shared function code, you can place in the shared code folder which is imported as modules in the main function _init_.py file.
You can import all the packages/libraries/methods derived in the normal class files into Azure Functions Python using the import keyword (absolute and relative references):
from shared_code import my_first_helper_function #(absolute)
import shared_code.my_second_helper_function #(absolute)
from . import example #(relative)
Please refer here for more information.
I know that this question has already been asked. But answers below these questions doesn't fix my problem. Here it is:
When I download some code from GitHub, it's always divided into separate files. I understand that it's important to have organized code, which is why I'd like to do the same.
However, whenever I try importing a function from a file, I always seem to get a ModuleNotFoundError error.
The file that I'm trying to import is in the same directory as the file importing the code. This also doesn't work with other code, for example, when I download code from GitHub that organizes code using separate files, it still returns the same error.
I've tried two different python installations (anaconda 3.7.3 and py 3.7.0), but still not luck. FYI I use pzyo to run my files.
Here's an example of how I import another file:
from fun import f
I have tried this as well:
import os
os.chdir("C:/Users/amau4/Desktop/test")
from fon import f
How would I go about fixing this? Thanks in advance!
I am heavy user of the pandas library.
In order to keep useful custom made helper functions related to pandas library, I decided to create a custom project (my_proj) and a module pandas.py in it.
Now I am developing another custom module related to ssh protocol in the same project.
Modules are created with pycharm. Structure of the project is as follows:
my_proj/src/my_proj/pandas.py
my_proj/src/my_proj/ssh.py
Everythin is OK and works properly. When I want to use site package's pandas I execute import pandas as pd, when I want to use my_proj pandas, than I use from my_proj import pandas as mypd.
But, now in ssh.py I need site package's pandas (not my_proj pandas).
If in ssh.py I use import pandas as pd, pycharm imports my_proj/src/my_proj/pandas.py instead of the pandas from site packages.
One solution would be to rename my_proj's pandas.py to something else, but I would like to avoid that if possible.
Is there another option to prevent loading library from current directory and import it from site packages?
What are my options?
At the end I will use the following procedure:
import sys
old_syspath = sys.path
sys.path = [path for path in sys.path if 'customspace' not in path]
import pandas as pd
sys.path = old_syspath
Basically, I am removing 'customspace' from sys.path, than importing pandas and puting sys.path back as it was.
I think this is minimal change and it is working.
I think that you could tell python exactly what directory to pull pandas from. EX from my_proj/src/my_proj/ssh import pandas.
I am fairly new to Jupyter Notebook. I have played around with it for a while. But this was my first time trying to import another notebook into my main class.
For reference, I am using Anaconda 4.3.1 and Python v2.7.
I am trying to replicate what I did in my python project to jupyter notebooks. It requires importing other .ipynb files (translated from the original .py files) into it to use relevant methods as desired.
For this, I followed the directions as given on Jupyter Nbviewer Steps Link which I found through my preliminary search on the following stack Question. It gave me some idea but didn't help me after one stage.
I will walk you through the steps I took and the sample program I tried.
Created a small .ipynb file abc.ipynb as follows
def prt_n(str):
print(str)
if __name__ == '__main__':
prt_n("in abc")
Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.
Run importer.ipynb
import abc
str="Hello Me"
Test step abc.__name__ results in abc as output.
abc.prt_n(str) throws following error
*---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2fb88a43c9e5> in <module>()
----> 1 abc.prt_n(str)
AttributeError: 'module' object has no attribute 'prt_n'*
I was hoping that the answer will be Hello Me.
Later, I want to actually create a myMain.ipynb file and in that I want to include 2-3 such notebooks and call their methods with required parameters.
One such example can be a file efg.ipynb as follows:
import abc
a="Hello Notebook"
abc.prt_n(a)
I also want to make sure if there is any other way to do this?
Note: I have already checked sys.executable and sys.path. Both have the same python path value.
Any sort of help is welcome!
Simple way to use ipynb files in jupyter note book are as follows:
1) Install import-ipynb
pip install import-ipynb
2) Import import_ipynb in jupyter notebook. Then import ipynb file as you import .py file
import import_ipynb
from test import print_name
print_name("your name")
**
Link to sample files on Drive
**
Ok. So, after some struggle and looking around on Internet, and finally found a solution that worked for my sample case.
Firstly, this is the stackoverflow question that was the most helpful to me. Mohideen and Tyhela's answer was the actual solution and not the one with most number of votes.
So, what I did was, I made a folder by the name module and placed all my .ipynb files there. Plus, I created an __init__.py file in that module by using touch __init__.py command such that the import can register it as a valid module. Those guys have given a detailed explanation for it which seems legit.
Then from my working directory I ran the following commands:
str = "Hello Me"
import test.abc as tabc
tabc.prt_n(str)
that gave me Hello Me in the output.
And for,
`import test.efg as tefg`
I got
importing Jupyter notebook from test/efg.ipynb
Hello Note
As my desired output.
I hope this will be of help to others who land up on similar problem.
If you have a better approach then I will appreciate if you can share that with me.
Thank you :)