ModuleNotFoundError when import from constants file in python - python

app-main-folder
/local
/__init__.py
/run.py
constants.py
I am trying to import from constants in run.py it's throwing this error
Traceback (most recent call last):
File "local/run.py", line 4, in <module>
from init import app
File "/home/manavarthivenkat/ANUESERVICES--BACKEND/local/init.py", line 5, in <module>
from constants import BaseConstants
ModuleNotFoundError: No module named 'constants'

pip install constants
Try this on your shell
and try running run.py
make sure you load the constants library

this is because Python assumes all your python files are in the current directory. you should tell the compiler you are looking for the file somewhere else.
from app-main-folder.constants import constants # assuming constants is the name of your class in that constants.py

You can use sys.path.append to tell Python interpreter where to search the modules.
First, you can check the current Python path
import sys
from pprint import pprint
pprint(sys.path)
If the path to the directory of constants.py is not included in the output, you can manually add that path, by
import sys
sys.path.append("/path/to/constants.py")

Related

ImportError and ModuleNotFoundError: how to get a script running from the command line?

I have a project with the following structure:
HorticulturalSalesPrediction/
Docker
HorticulturalSalesPrediction_API/
optimization/
__init__.py
optuna_optim.py
preprocess/
__init__.py
base_dataset.py
utils/
__init__.py
FeatureAdder.py
helper_functions.py
__init__.py
optim_pipeline.py
run.py
In the script run.py I import stuff like this:
import optim_pipeline
from utils import helper_functions
And in the script optim_pipeline.py I import stuff like this:
from utils import helper_functions
from preprocess import base_dataset
from optimization import optuna_optim
I developed this framework with the IDE PyCharm and when I run it with the 'Run'-Button, the framework works fine. But when I want to run it over a terminal with python3 run.py or python3 -m run.py, I get the following error:
Traceback (most recent call last):
File "run.py", line 3, in <module>
import optim_pipeline
File "/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/optim_pipeline.py", line 4, in <module>
from preprocess import base_dataset
File "/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/preprocess/base_dataset.py", line 8, in <module>
from HorticulturalSalesPrediction_API.utils import FeatureAdder
ModuleNotFoundError: No module named 'HorticulturalSalesPrediction_API'
I know that there are already tons of questions and solutions to this whole python import topic (Relative imports - ModuleNotFoundError: No module named x, Call a function from another file?, Relative imports for the billionth time, ...), but none of these worked for me.
When I print sys.path I among others receive '/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API', so all this stuff should be available at the syspath.
I also tried to do relative and absolute imports. But with these attempts I reveice ValueError: attempted relative import beyond top-level package or ImportError: attempted relative import with no known parent package errors (e.g. when I try from . import optim_pipeline).
Try reinstalling the modules and updating python.
Update HorticulturalSalesPrediction_API
So I found my mistake.
The solution is to run python3 -m HorticulturalSalesPrediction_API.run in the HorticulturalSalesPrediction folder.
Then it works like expected.
I just then had to adjust some imports:
from HorticulturalSalesPrediction_API.utils import helper_functions
from . import optim_pipeline
and
from HorticulturalSalesPrediction_API.utils import helper_functions
from HorticulturalSalesPrediction_API.preprocess import base_dataset
from HorticulturalSalesPrediction_API.optimization import optuna_optim

I have coded to import python modules into main.py . But I have problem to import

E:\Karya\Python <-- main.py inside here
E:\Karya\packages\extra< -- iota.py inside here
import sys
sys.path.append('E:\Karya')
sys.path.append('E:\Karya\Python')
from ..packages.extra import iota
print(iota.FunI())
Question:call function FunI() by import iota.py
ERROR :
$ C:/Users/ready/AppData/Local/Microsoft/WindowsApps/python3.9.exe e:/Karya/Python/main.py
Traceback (most recent call last):
File "e:\Karya\Python\main.py", line 5, in
from ..packages.extra import iota
ImportError: attempted relative import with no known parent package
detail:
tree directory, main.py inside python folder and iota.py inside extra folder
Error
Code
Just add packages directory to sys.path and import iota from extra
sys.path.append("E:\\Karya\\packages")
from extra import iota
Make sure that there is init.py file in extra directory (and I think it is needed in packages directory too)

ImportError: No module named common

My project has two dirs common and core.
root
|----common
| |-----__init__.py
| |-----util.py
|
|------core
|-----__init__.py
|------iemoji.py
In root, I execute python core/iemoji.py, an error occurs.
Traceback (most recent call last):
File "core/iemoji.py", line 6, in module
from common import util
ImportError: No module named common
I import utils.py like this:
from common import util
It looks like /path/to/root/ is not on your python path when you call python core/emoji.py. You can check by printing sys.path in your script.
import sys
print(sys.path)
You could add the root directory to your python path by setting the PYTHONPATH environment variable:
PYTHONPATH=/path/to/root python core/emoji.py
or you could set it in your script:
import sys
sys.path.append('/path/to/root')
Set up your __init__.pys to point to the modules in their respective folders.
common's __init__.py:
from . import util
core's __init__.py:
from . import emoji
Then you should be able to call it with:
from common import util
Let me know if this works.
add this export PYTHONPATH="$PWD" before python core/iemoji.py
pip install common, use this command and install the package by using pip.

cannot find module in another directory

I have the project structure:
/hdfs-archiver
/logs
/qe
__init__.py
/tests
__init__.py
archiver.py
/utils
__init__.py
HdfsConnector.py
I am trying to run archiver.py but I am getting this error:
Traceback (most recent call last):
File "qe/tests/HdfsArchiver.py", line 8, in <module>
from qe.utils import HdfsConnector
ImportError: No module named qe.utils
I read around and it seemed like most people that come across this issue fixed it with __init__.py
when I pwd:
$ pwd
/Users/bli1/Development/QE/idea/hdfs-archiver
my PYTHONPATH in .bashrc
export PYTHONPATH=$PYTHONPATH:/Users/bli1/Development/QE/idea/hdfs-archiver
I also tried having my PYTHONPATH as
/Users/bli1/Development/QE/idea/hdfs-archiver/qe
You're trying to import HdfsConnector as a function or class. Include the HdfsConnector module as part of your absolute import:
from qe.utils.HdfsConnector import my_function
You can also import the module:
import qe.utils.HdfsConnector
# or
import qe.utils.HdfsConnector as HdfsConnector
Firstly, you could try a relative import such as
from ..utils import HdfsConnector
You'd also need to run the script as a module and not as a simple python script due to the __name__ being different. This wouldn't require you to modify the path.
You can find more info here.

Python imports with __init__.py

No matter how I structure the imports in the code files and in the __init__.py files, I can't seem to get it right for executing the program and running the tests using pytest. How do I need to write the imports when my project structure looks like this:
src/
__init__.py
VocableFileWriter.py
WordListHelper.py
WordListReader.py
XLDAttributeValueAdder.py
exceptions/
__init__.py
XMLInvalidException.py
XMLParseException.py
gui/
__init__.py
GTKSignal.py
XLDAttributeValueAdderWindow.py
test/
__init__.py
test_XLDAttributeValueAdder.py
xmlparser/
__init__.py
XMLParser.py
Currently I have them like this:
In the __init__.py files I have the imports like this (src/__init__.py):
from src import *
from src.exceptions import *
from src.xmlparser import *
and in a subpackage (src/xmlparser/__init__.py):
from src.xmlparser import *
So I guess those are project-absolute paths to the modules.
In the code files themselves I import like this:
import os
import sys
from VocableFileWriter import VocableFileWriter
from XLDAttributeValueAdder import XLDAttributeValueAdder
However, when I execute the code from the directory above src using:
./src/main.py
It tells me that:
Traceback (most recent call last):
File "./src/main.py", line 6, in <module>
from VocableFileWriter import VocableFileWriter
File "/home/xiaolong/Development/PycharmProjects/xld-attribute-value-adder/src/VocableFileWriter.py", line 2, in <module>
from XMLInvalidException import XMLInvalidException
ImportError: No module named 'XMLInvalidException'
It used to be a PyCharm project, but I couldn't get it to run with the imports structure PyCharm used when not using PyCharm but running it from the terminal, so I decided I wanted to take the whole import stuff into my own hands. So don't get confused by it being in a PyCharm directory.
I also want to be able to execute the tests using for example:
py.test src/test/test_XLDAttributeValueAdder.py
How do I solve this mess?
Edit#1:
I had the program running once, but then the test complained about missing modules, probably because it's in another subdirectory and I tried so many configurations, that I don't know how I had the program running anymore. If possible please add some explanation why a certain structure is correct and works for both, tests and the program itself.
EDIT#2:
I've managed to get the tests running now, but now the program doesn't run anymore.
I emptied all the __init__.py files and used only project-absolute paths in the code files like this (src/test/test_XLDAttributeValueAdder.py):
from src.VocableFileWriter import VocableFileWriter
from src.WordListHelper import WordListHelper
from src.WordListReader import WordListReader
from src.XLDAttributeValueAdder import XLDAttributeValueAdder
from src.exceptions.XMLInvalidException import XMLInvalidException
from src.exceptions.XMLParseException import XMLParserException
from src.xmlparser.XMLParser import XMLParser
from src.test.path_helper import go_up
from src.test.path_helper import go_in
and in the main.py:
from src.VocableFileWriter import VocableFileWriter
from src.XLDAttributeValueAdder import XLDAttributeValueAdder
the output of ./src/main.py:
./src/main.py
Traceback (most recent call last):
File "./src/main.py", line 6, in <module>
from src.VocableFileWriter import VocableFileWriter
ImportError: No module named 'src'
EDIT#3:
I've tried the relative import way like this:
main.py moved one directory up
The structure now looks like this:
main.py
src/
__init__.py
VocableFileWriter.py
WordListHelper.py
WordListReader.py
XLDAttributeValueAdder.py
exceptions/
__init__.py
XMLInvalidException.py
XMLParseException.py
gui/
__init__.py
GTKSignal.py
XLDAttributeValueAdderWindow.py
test/
__init__.py
test_XLDAttributeValueAdder.py
xmlparser/
__init__.py
XMLParser.py
__init__.py files empty
main.py file gets relative
Imports look like this:
import os
import sys
from .src.VocableFileWriter import VocableFileWriter
from .src.XLDAttributeValueAdder import XLDAttributeValueAdder
When I try to run the main.py file:
Traceback (most recent call last):
File "main.py", line 6, in <module>
from .src.VocableFileWriter import VocableFileWriter
SystemError: Parent module '' not loaded, cannot perform relative import
However, there is a __init__.py file in the same directory as the main.py file, also empty.
You'd better use relative imports.
When doing from src import * in src/__init__.py you'll only import anything you've defined before calling the import. Most likely that's just nothing. If you've defined the __all__ variable, you'll get the submodule from it, but maybe also an AttributeError if a variable hasn't been defined yet.
So instead import the modules you need explicitly where you need them like
from .VocableFileWriter import VocableFileWriter
from .exceptions.XMLInvalidException import XMLInvalidException
or lets say within src/gui/GTKSignal.py
from ..exceptions.XMLInvalidException import XMLInvalidException
Also you could use project-absolute paths like mentioned in your Edit#2.
Furthermore you've a problem with your path when calling ./src/main.py. In this case the directory containing src is not in the path. Instead ./src/ is in the path. Therefore python doesn't consider it a package anymore and since there's no other package with that name available, the import fails.
Instead put your main.py module in the same directory like the src package or call it as a module with python -m src.main. Doing so you could also rename it to src/main.py and call the package instad with python -m src.
When using the first approach, main should use an absolute import structure. Just think of it as a module you put anywhere on your computer, while your src package is somewhere else, where it can be found by Python, like any other module or package installed by the system. It's not different, when it's located besides the package itself, since the current directory is in sys.path.
main.py
import os
import sys
from src.VocableFileWriter import VocableFileWriter
from src.XLDAttributeValueAdder import XLDAttributeValueAdder

Categories