Jupyterhub - import data from another file - python

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.

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

How to fix ModuleNotFoundError in a Python file

I have a small issue regarding importing a class from another python file.
Here is the file that I try to import. The EvaluationDetect is a class from the evaluation_detect.py file. The file evaluation_detect.py is located into path
from oculus_evaluation.oculus_evaluation.evaluation_detect import EvaluationDetect
And Pycharm IDE always shows me this error message.
ModuleNotFoundError: No module named 'oculus_evaluation.oculus_evaluation'; 'oculus_evaluation' is not a package
Please I do not understand where is the wrong in this case. There is not possible to import a class. This a legacy code and I do not understand.

Import modules created locally

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()

python. package works in folder A but not in folder B

A noob python question.
I have a package called TorCtl, I saved it in /root/A and create a file (useIt.py) in /root/A which uses the package and it works absolutely fine.
now the problem starts when I try to use this package within another project.
I copied TorCtl package into /root/B/C and created a new file (useIt2.py) (this file is used from another file that is the main for this package.)
when I run main I get import error from TorCtl package. for example:
from elixir import *
ImportError: No module named elixir
I have no idea why it should work in one folder and not another.
Can anyone point out what I am doing wrong please?

How to call a python file, that needs to import packages?

I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")

Categories