Python module not found - subdirectory - python

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 *

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

Import [Module] could not be resolved (PylancereportMissingImports), with module in the same folder/directory

The first few lines of the code of evaluation.py:
import os
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
import numpy as np
from dataset import CLSDataset # warning is reported here
from tqdm import tqdm
The structure of the folder:
./
|-dataset.py
|-dictionary.py
|-evaluation.py
|-model.py
|-models/
|-[some files]
|-__pycache__
|-train.py
Notice that dataset.py is in the same folder as that of evaluation.py and https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings says that The language server treats the workspace root (i.e. folder you have opened) as the main root of user module imports. But it still throws an warning of "Import dataset could not be resolved".
I tried to add the
{
"python.analysis.extraPaths": ["./"]
}
on the settings.json of both local and remote files, but it does not help.
In VSCode, go to the main window and do the following:
Do Ctrl+Shift+P (for Windows) and Command+Shift+P (for Mac)
Scroll and go to Python Select Interpreter
Now select whichever python version is installed in your system.
You're good to go!
Do upvote if it helps you!
Dataset is a relative import.
Add an __init__.py file your directory (a file with no content). Then try:
from .dataset import CLSDataset
That being said, Python imports are a tricky business. It looks like we’re only getting a glimpse of one part of workspace setup.
(I am surprised that your code runs as-is.)
Consider scaffolding your project with cookiecutter. Also try out the vs code python project tutorials. You don’t have to remake your entire project but these will give you a starting point for understanding where your current project goes awry.
I recently had the same error, just try restarting the IDE. You probably installed it thought the code was already open. Or you haven't installed the module at all.
Edit:
I'm sorry I understood it wrong, please send the code.
Edit2:
You need to add the .py at the end of the import if it is a .py file

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 import modules issue

I recently was asked to deliver a python project as part of an interview process.
I tested my project on Windows and MacOSX, with Pycharm, Spyder, jupyter notebook and command line and everything works fine.
However, the reviewer was unable to make the project work on his side, because of module import issues according to him.
My modules are organized like this:
my_project/
my_module.py
main_module.py
my_package/
__init__.py
my_submodule_1.py
my_submodule_2.py
my_submodule_1.py:
import my_module
import my_submodule_2
I haven't added any path related to this project in PYTHONPATH.
The project main function is located in main_module.py.
The reviewer seem to have problems with the modules imported in my_submodule_1.py.
Could anyone shed some light on possible mistakes here and why it would work on my side and not on his?
Your my_submodule_1 module is doing an implicit relative import when it imports my_submodule_2 directly.
That's not legal in Python 3. It is allowed in Python 2, though its usually a bad idea to use it. You can get the Python 3 semantics by putting from __future__ import absolute_import above the other import statements in your file. To fix the import, you'd want to change import my_submodule_2 to either import my_package.my_submodule_2 (an absolute import) or from . import my_submodule2 (an explicit relative import).
If your interviewer is using Python 3 and you're using Python 2, there are likely to be other issues with your code (especially if you're doing any sort of text processing), so I'd make sure you're testing your code in the version they expect!
I think since my_module.py is not in same directory as my_submodule1.py ,and on the reviewer pc the sys.path doesn't have that location of my_module.py, that's why it getting problem in importing the module from its parent directory.
if u give the details of error that the reviewer is getting it might help finding the right solution.

PyPy Sandbox: Cannot import rpython module

I'm trying to use PyPy to create a server-side sandbox with limited access to my file system. I am working on Ubuntu 12.04 64 bit machine and have been trying to install the full source code for PyPy from here: http://pypy.org/download.html#sandboxed-version (scroll down to the section "Building from source").
My problem is that whenever I try running pypy_interact.py (located in pypy/pypy/sandbox), I get the following error:
ImportError: No module named rpython.translator.sandbox.sandlib
The module that cannot be imported has the following path: pypy/rpython/translator/sandbox/sandlib.py. The contents of pypy_interact.py are as follows:
import sys, os
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..\
', '..', '..')))
from rpython.translator.sandbox.sandlib import SimpleIOSandboxedProc
from rpython.translator.sandbox.sandlib import VirtualizedSandboxedProc
from rpython.translator.sandbox.vfs import Dir, RealDir, RealFile
import pypy
LIB_ROOT = os.path.dirname(os.path.dirname(pypy.__file__))
I feel like this is a really simple fix -- I just started learning Python a few days ago so I'm not exactly sure how to go about fixing the issue/don't understand imports too well yet. Any advice? Thanks very much.
Rpython typically expects that you set PYTHONPATH to include the root of your pypy checkout and not mess with the sys.path.
So you typically call the script via
PYTHONPATH=$PYTHONPATH:path/to/pypy/source path/to/pypy_interact.py

Categories