Import modules created locally - python

I am trying to learn how to import modules in python that are created locally. Below is a module that I created and saved in the python folder on my local disk.
When I try to call this module in another piece of code, I get an error-
I am using Jupyter notebook and both the module and code calling the module are in the same directory.
Can someone advise what I am doing wrong here?

can you try this?
import sys
sys.path.append('C:/Users/hchopra/Desktop/Python-Folder')
import myModule as m
m.fish()

Related

VS Code throws ModuleNotFoundError despite folder available

I'm working on creating a Python/PySpark library using VS Code. My goal is to debug in VS Code and create a .whl package to be installed in a Databricks cluster. I face the following situations:
if I use from checkenginelib.pysparkdq._constraints._Constraint import _Constraint I get a ModuleNotFoundError in VS Code and a module not found error in Databricks
if I use from pysparkdq._constraints._Constraint import _Constraint I get a ModuleNotFoundError in VS Code but all imports work well in Databricks
if I use from _constraints._Constraint import _Constraint I get no error in VS Code but I get a module not found error in Databricks
Because your module dqengine is not in the top level folder, it is probably not in your PYTHONPATH variable, which VSCode has probably added the path to DATA QUALITY ENGINE
Either:
Move it to the top level folder (Data quality engine)
add the path to check_engine_lib to PYTHONPATH.
Or as #franjefriten says, add an __init__ to check-engine-lib and do
from check-engine-lib.dqengine.validate_df import *
From what I see, you are working in ./DATA-QUALITY-ENGINE/check-engine-lib/dqengine/validate_df. You have to import it the following way:
from check-engine-lib.dqengine.validate_df import *
That should work. Also you need to create a \__init__.py file to import other files as modules
As the comment said, when the method you need to import is in the same directory as the current file, you only need to import it directly.
from validate_df import _Constraint

Question about Colab importing your own modules

I am relatively new to Colab & python, so I don't really know how to use it. I have git-cloned a GitHub repository onto my Colab notebook. There are some modules inside the GitHub folder that I have cloned that I need to import them into the notebook. For example, there is a utils and models module that I want to import. I ran the code:
from models import *
from utils import *
However, the Colab editor wrought the error ModuleNotFoundError: No module named 'models' and ModuleNotFoundError: No module named 'utils'. This led me to realize that I haven't imported the modules from the Git Hub clone in Colab Files into Colab. I couldn't find an answer on StackOverflow, w3schools, or the Colab official sites so I
wrought my question here. If any of you have a link to a guide or a solution, please help me. Thank you!
Yes, some of the Jupyter Notebook tricks worked. I used %cd pytorch_objectdetecttrack to get to the modules.
Not an expert in Colab, but for Python import only works for Python modules. So if there's a specific script you're trying to import, you will have to call it like import folder.someModule. The folder you're trying to access should go first, then the module name.
It's the same as saying ./folder/someModule.py.
tl:dr; You have to specify the path to the module if it's not in the current working directory.

Python module not found - subdirectory

My main script reads another script that lies in a sub-folder "models".
Codes had been working perfectly until recent tech refresh/whole machine updates.
Error reads: Module not found. Error also happens when I try to import a library which ran perfectly previously. No issue with importing other libraries like tensorflow & keras though. Suspect issues with calling path directory but not sure how to approach and resolve.
from models.model import *
import pdf2image
The project structure is as follows. I will run mainscript.py for this project.
/project/mainscript.py
/project/models/model.py
Any guidance is much appreciated!
It is good to edit python include path.
import os, sys
sys.path.append(f"{os.path.dirname(__file__)}/models")
from models import *

Jupyterhub - import data from another file

I am new to jupyterhub, and I'm trying to do the following.
So have a code on jupyterhub, that is supposed to do the following:
from a import b
where a is another file in the same "Folder". However, it's not working as it usually works on Spyder, and I'm receiving the following error message:
ImportError: No module named 'a'
How could I import data or functions from another files on jupyterhub?
I found a way to do this.
Simply I create the module on Spyder, and saved it as a.py, then uploaded it to JupyterHub.
Then from the original source code I did
from a import b
and it worked correctly.

How can I import python custom class in Anaconda (Jupyter notebook)

I don't manage to find how import custom class in Python with the Jupyter notebook in anaconda.
In my work folder I have a file 'user.ipynb' that contains a class name User. In an other file in the same folder, I try to import this class with : from user import User.
I get this error: ImportError: No module named user.
I tried to create a file ' _ _init__.py' and _ _init__.ipynb in this folder but it doesn't work.
Do you know how I could do this ?
Thank you in advance
Python modules are either files named *.py or directories with an __init__.py in them. user.ipynb is neither, it's an IPython (Jupyter) notebook, so it is not a module and you can't import it.
The easiest way to get this to work is to convert the notebook to a .py file (this can be done using the export function) and then you'll be able to import it.
An alternative is to provide code that lets you import notebooks directly; such code is available here. Looks like a lot of bother, though. I'm not sure why they didn't just bake this in; it seems like it would be useful.

Categories