Google Colab - Importing Python Scripts as Modules!

Поделиться
HTML-код
  • Опубликовано: 12 авг 2024
  • Brief tutorial on how to import a Python script as a module within Google Colab. When importing a file from Google drive, be sure that the notebook within Colab is connected to the drive.
    Tutorial on Importing Jupyter Notebooks as Modules - • Google Colab - Importi...
    The notebook can be found in the "Google Colab Tutorials" folder within the below repo.
    GitHub Repo - github.com/ad17171717/RUclips...
    CONNECT:
    LinkedIn: / adrian-dolinay-frm-96a...
    GitHub: github.com/ad17171717
    Twitter: / dolinayg
    Odysee: odysee.com/@adriandolinay:0
    Medium: / adriandolinay
    |-Video Chapters-|
    0:00 - Intro
    0:07 - Creating a Python script to import
    0:54 - Method 1: Importing a script from Google Drive
    2:43 - Method 2: Importing a script from your local computer

Комментарии • 60

  • @PAINTBALLA728
    @PAINTBALLA728 Год назад +2

    I never feel the need to comment because I think a like and a subscribe is enough. But man, Thank you for your page and the time you have put towards the videos. I just took a look through the "All about Python" playlist and I'm speechless. I hope your profile continues to grow by the day! Blessings!

  • @khanhduyphan6091
    @khanhduyphan6091 Год назад +3

    as a newbie in python code, i feel really grateful your work. This first step took me a night.

  • @albertosorianogomez5786
    @albertosorianogomez5786 2 года назад +7

    Thank you! Super clear and useful explanation!

  • @shifanasrin2748
    @shifanasrin2748 3 месяца назад

    best python video in this topic I have seen

  • @dario27
    @dario27 7 месяцев назад

    Straight and to the point. Excellent tutorial.

  • @tsi.
    @tsi. Год назад +1

    Thank you so much! Your video is so easy to follow and you helped me a lot on my school activity! Do take care!!!

  • @anilnaveendavid
    @anilnaveendavid Год назад +1

    Sir , simple and powerful, thank you some much for making this video .

  • @gatoro59
    @gatoro59 Месяц назад

    Muchas gracias. Thanks you genius....!! Simple y rápido...

  • @Antz_411
    @Antz_411 2 года назад +2

    I am having major issues with calling a function from a script in a sub-folder.
    Does anyone know how to accomplish this simple task in Colab?
    For example, I need the "Preprocessor" function from the "preprocessing.py" file inside the "util" directory:
    from util.preprocessing import Preprocessor
    It does not seem to be that simple??

  • @aadilzikre
    @aadilzikre Год назад

    Very Helpful and to the point. Thankyou!

  • @santiagoarango6145
    @santiagoarango6145 2 года назад

    Thank you very much! Have an amazing day!

  • @BlinkDrive555
    @BlinkDrive555 2 года назад

    The first method is working well for me. Thank you very much

  •  2 года назад +1

    So helpful! Thanks!

  • @vohiepthanh9692
    @vohiepthanh9692 Год назад

    Many thanks bro, your video help me a lot.

  • @LearningWorldChatGPT
    @LearningWorldChatGPT 2 года назад

    Amazing your video. Thank you so much!

  • @hashimalsadah3541
    @hashimalsadah3541 7 месяцев назад

    Great content, thank you.

  • @VikramSai.
    @VikramSai. Год назад

    thanks a lot. very helpful.

  • @Asadkhan-nz6qm
    @Asadkhan-nz6qm 2 года назад

    Thank you, Sir .

  • @sukanya4498
    @sukanya4498 2 года назад

    Excellent!

  • @ashwinkumar5223
    @ashwinkumar5223 Год назад

    I want to use config python script declared variable in user define function parameter like fun_name(config.val_path) in google colab notebook.

  • @lorenzoandreasfrisch4947
    @lorenzoandreasfrisch4947 2 года назад +1

    grazie bro

  • @fawazbashiru6165
    @fawazbashiru6165 11 месяцев назад

    Thank you so much

  • @banvaakib1857
    @banvaakib1857 Год назад

    Thank you sir

  • @sycs32-aayushrajbhar
    @sycs32-aayushrajbhar Год назад

    Thank you very much 😄

  • @EmphaticTrain
    @EmphaticTrain Год назад

    say i have a folder named "utils" in google drive. In this folder i have several .py files -- i want to call "from utils import ___.py" -- this does not work with the methods you mentioned. any help please?

  • @jumpshot6924
    @jumpshot6924 2 года назад

    its useful but the screen display is not clear

  • @santiagoacevedo4094
    @santiagoacevedo4094 2 года назад

    Thank you!

  • @amirhosseinghasemi6252
    @amirhosseinghasemi6252 2 года назад

    how can i open a .py file completely in colab? (scrips as well)

  • @8003066717
    @8003066717 2 года назад

    is there an difference between module and package because i have __init__.py in module?

  • @8003066717
    @8003066717 2 года назад

    i have .py file module ,there are many files in the folder and i have uploaded that folder in gdrive and firstly i have mounted the gdrive in collab but while importing particular folder i getting no module found error.
    so how to deal with it??

    • @AdrianDolinay
      @AdrianDolinay  2 года назад +2

      You can use the OS module to navigate to the folder to import the py file. For example if my “sqroot” py file is in a folder called “Test” I can run the code below:
      import os
      os.chdir('/content/drive/MyDrive/Test')
      import sqroot

  • @anshulsinghal914
    @anshulsinghal914 9 месяцев назад

    I have code folder with many .py files how to run that in google colab

    • @AdrianDolinay
      @AdrianDolinay  9 месяцев назад

      You can upload your Python files to Google drive and import them from there. The tutorial below walks through this, hope it helps!
      ruclips.net/video/6UnCrulz-fE/видео.html

  • @ashwinkumar5223
    @ashwinkumar5223 Год назад

    But I am getting error

  • @sajjad5345
    @sajjad5345 Год назад

    thanks alot

  • @brianyeh2695
    @brianyeh2695 2 года назад

    thank you

  • @adityagoel237
    @adityagoel237 2 года назад

    While importing, I get an error - NameError: name 'null' is not defined. Stackoverflow says that this is because I simply renamed my .ipynb colab file to .py file which is not acceptable. The file should be written in .py from the beginning. How to write a .py file in colab from the beginning?

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      To save a Google Colab Notebook as a .py file, you can select: "File" -> "Download" -> "Download .py".
      At 0:38 in the video I save the "squareroot" notebook as a .py file.

  • @sagarikaghosh6532
    @sagarikaghosh6532 2 года назад

    can you write down the code for importing a .py file from Google drive because screen is not visible

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      You can check out the code by following the GitHub link below. Hope this helps!
      github.com/tudev/Workshops-2020-2021/blob/master/Google%20Colab%20Tutorials/Google%20Colab%20%20-%20Importing%20Python%20Scripts%20as%20Modules!/Google_Colab_Importing_a_Python_Script_as_a_Module.ipynb

  • @abdalrahman5012
    @abdalrahman5012 2 года назад

    am having issue, what i want to do is importing class from other python file so for example am in file x so : from y import yclass, so i get error that y is not resloved, do you have any idea how to resolve it?

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      If you share the script/notebook on GitHub I can take a look

    • @abdalrahman5012
      @abdalrahman5012 2 года назад

      @@AdrianDolinay here you go github.com/Beartx/facial-recognitionx

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      @@abdalrahman5012 are you running this in Google Colab or on your local machine? If you're running it from a local machine you can use the os package to get the path in order to import you .py file.

    • @abdalrahman5012
      @abdalrahman5012 2 года назад

      @@AdrianDolinay i mount the code from my google drive, so i don't need to upload the code each time, running it through colab directly seems to work but when i mount it with google drive it is like not recognizing the imports of other files

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      I think you may need to check the folder your file is in to get the right path. You can get the path by going into the drive folder on the right hand side, right click the file and select "Copy path" to get the correct path. Unfortunately without seeing the notebook/drive I'm not sure what else could be the issue

  • @RajaGopal_Tangella
    @RajaGopal_Tangella 2 года назад

    Hi Adrian,
    How can I use a function that I've created in another.ipynb in my current.ipynb in Colab?
    I have mounted my gdrive into current ipynb. Can you suggest next steps.

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      Making a video on this now, it should come out later today or tomorrow. In the meantime you can check out the GitHub repo below which you can use to import notebooks as modules. Hope this helps!
      github.com/axil/import-ipynb

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      Here's the tutorial on how to do it, hope it helps!
      ruclips.net/video/sJFHCQPxclw/видео.html

  • @AK-ff4gv
    @AK-ff4gv 2 года назад

    thank you! can u import it as .ipynb directly without having to go through saving it as .py?

    • @AdrianDolinay
      @AdrianDolinay  2 года назад +1

      Yes! I made a video on importing functions from a ipynb file, check out the link below
      ruclips.net/video/sJFHCQPxclw/видео.html

  • @SP-db6sh
    @SP-db6sh 2 года назад

    How to install .whl file in colab?

    • @AdrianDolinay
      @AdrianDolinay  2 года назад +1

      I have never used a wheel within Colab, but the below Stack Overflow post may help you
      stackoverflow.com/questions/27885397/how-do-i-install-a-python-package-with-a-whl-file

    • @SP-db6sh
      @SP-db6sh 2 года назад

      @@AdrianDolinay Thank you! Wheel docs also helpful.

    • @SP-db6sh
      @SP-db6sh 2 года назад

      @@AdrianDolinay After unpacking with wheel how to install it using pip3?

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      @@SP-db6sh Again I've never used it within Google Colab, but the below Reddit thread may help you out
      www.reddit.com/r/GoogleColab/comments/f3mzdn/installing_packages_in_google_colab_from_a_whl/

  • @manjunathpatkar477
    @manjunathpatkar477 2 года назад

    Hi Adrian, can you help with the below error:
    cp: cannot stat '/content/drive/MyDrive/Colab Notebooks/Coolingcoffee.ipynb': No such file or directory

    • @AdrianDolinay
      @AdrianDolinay  2 года назад

      Most likely you are not connected to Google Drive. Trying running the codeblock below. If you are already connected to Google Drive you need to double check that the path/filename are correct.
      from google.colab import drive
      drive.mount('/content/drive')

    • @AdrianDolinay
      @AdrianDolinay  2 года назад +1

      You can also check out the Colab notebook below which reviews in detail how to import data/packages/files into your current notebook
      colab.research.google.com/notebooks/io.ipynb#scrollTo=RWSJpsyKqHjH