change in import in python3 - python

I am using python 3.4.4 and testing "init.py" feature by creating a sample package But unable to implement. The mentioned case is working perfectly in case of python 2.7.13 version. Can anyone tell me the mistake i am doing or is there any change in syntax of python 3.x versions. Please help me to learn Python 3?
Dir Structure:
TestPackage/
__init__.py
TestModule.py
run.py
Content of TestModule.py :
def TestFun():
print("Welcome")
Content of __init__.py :
from TestModule import TestFun
Content of run.py :
from TestPackage import TestFun
TestFun()
When i execute run.py file, i got the following error:
Traceback (most recent call last):
File "D:\CASE03\run01.py", line 1, in <module>
from TestPackage import TestFun
File "D:\CASE03\TestPackage\__init__.py", line 1, in <module>
from TestModule import TestFun
ImportError: No module named 'TestModule'
But when i use python 2.7.13 it works perfectly fine. Please guide me.

Inside of __init__.py, if you change
from TestModule import TestFun
to
from .TestModule import TestFun
You'll get the expected behavior.
See: PEP 328 (sections regarding relative imports using leading dots).

Try changing the __init__.py to the below code:
from TestPak.TestModule import TestFun

The simplest solution is to set __init__.py as a blank file. In case you are interested in controlling what gets imported from your module when you do from Testmodule import *, you can include __all__ = ['TestFun'] in __init__.py file.

Related

ModuleNotFoundError when import from constants file in 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")

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

Python3.6 ImportError when running script outside the directory. Runs correctly within same directory

I have the following set up.
~/python/pyct/lib/
├── printer.py
└── utils.py
~/apps/proj/
└── main.py
~/python/pyct/lib/utils.py
def printFunc(content):
print(content)
~/python/pyct/lib/printer.py
import utils # this breaks sometimes
# import pyct.lib.utils as utils # this works always
def printer(content):
utils.printFunc(content)
~/apps/proj/main.py
from pyct.lib.printer import printer
printer("hi")
Value of PYTHONPATH=$HOME/python
When I import printer.py in a file inside ~/python/pyct/lib/, everything runs as expected.
When I run main.py from ~/apps/proj/, I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from pyct.lib.printer import printer
File "~/python/pyct/lib/printer.py", line 1, in <module>
import utils
ModuleNotFoundError: No module named 'utils'
I have tried using relative imports but that doesn't work. I have gone through this resource but could get the scenario working.
I am aware that the problem can be solved using absolute path in imports but I want to make a dir with multiple python files which will act as a library. Then use the core component else where outside the directory.
How can I achive the above scenario working.
Any resources to understand python imports will be very helpful.
The problem here is that ~/python/pyct/lib/printer.py looks for module named utils in its working directory - not in directory where it is placed itself.
You can use relative import in ~/python/pyct/lib/printer.py:
from . import utils
python will then look for module relative to the path of the importing one instead of working directory.

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 cannot import module from subdirectory even with a file named __init.py__ in the directory

I know this question has been asked many times here and I'v probably read most of the answers (including this and that) as well as the python documentation but still can not find an answer to my very simple import problem. It's so simple that I must miss something stupid but I don't see it yet.
I have setup the following structure:
myproject
myscript.py
MyPackage
__init.py__
mymodule.py
I just want to load mymodule.py from myscript.py (or the commandline python interpreter which should be the same).
myscript.py contains:
#!/usr/bin/python
import MyPackage
__init.py__ contains:
from . import mymodule
mymodule.py contains
#!/usr/bin/python
def myfunction():
print "mymessage"
My goal is to call myfunction from myscript.py but if I try to call the module I get
$python myscript.py
Traceback (most recent call last):
File "myscript.py", line 2, in <module>
import MyPackage
ImportError: No module named MyPackage
What I already tried:
I tried everything under OSX and Ubuntu Linux to reduce the possibility of a faulty python installation.
I set the PYTHONPATH environment variable to the myproject directory as well as to . and to both.
I left __init.py__ blank
I tried the import statements also from the python interpreter started from the myproject directory
I tried the following import statements:
from MyPackage import mymodule
import MyPackage.mymodule
import MyPackage.mymodule as module
all without success (same error message).
If I put mymodule.py in the project directory without using a package, import works fine. But I don't see why the import from the subpackages is not working.
Any idea how I can get that to work?
Thanks for help!
While editing the formatting of your post, I noticed you're calling your file __init.py_. That causes python to not recognize your MyPackage directory as a package, hence the ImportError: No module named MyPackage.
It should instead be __init__.py (Name __init__, extension .py). Then it will work, your project structure and import statements are otherwise correct.
One minor point though: You should also use lower_underscore style for naming your package. Packages are modules as well in Python, and they should follow the same naming conventions. See PEP8 for details on recommended style and naming conventions. This is just a convention though, it has nothing to do with your problem.

Categories