Python module import error without using sys module - python

I am trying to use my own custom module, but It is unable to load the module.
This is a hierarchy structure of my custom module.
sources\
set1\
module_1
module_2
set2\
module_3
set3\
module_4
I imported these modules like below
from sources.set1.module_1 import *
from sources.set2.module_2 import method
...
And it occurs import error.
The error message is No module named 'sources
I use VScode, and Python 3.7 (I expect that I don't need to use init.py)
I've googled this problem and I've found 2 solutions. However, these weren't helpful.
Using sys.path.append()
This couldn't be a solution for me. Because I am working with teammates, and it is not allowed to add this code just for me
Adding PYTHONPATH environment variable
I've already added PYTHONPATH with "C://directories//sources",but it doesn't soleve import error. However, I found that this solution allows below codes instead of the original codes.
import module_1 #This occurs no error, But I can't use it
...

You have to call as a packages, for that you have to use init,py in each subdirectory https://www.learnpython.org/en/Modules_and_Packages

Related

how to import module from same level directory in python

as the question, i have my directory map like this
the formatOutput contain some function for better print
now, i want use a function in module printChecked.py in package formatOutput from findInfoSystem.py
i have tried create __init__.py in all folder to treat python it is a package (the advice i get from previous answer other post) but it always failed.
case 1: from formatOutput.printChecked import print_checked_box
error is: ModuleNotFoundError: No module named 'formatOutput'
case 2: from dttn.formatOutput.printChecked import print_checked_box
error is ModuleNotFoundError: No module named 'dttn'
case 3: from ..formatOutput.printChecked import print_checked_box
error is ImportError: attempted relative import with no known parent package
i don't want to use the sys.path method because i think it is not the good way to solve problem.
Help me please !
There are two classes of solutions to this problem:
modify the python import path
use the various setup tools or manually install .egg-links or symbolic links
to "install" your development modules in the python/lib/site-packages
The first approach is simple, but the second involves delving deeper
into a number of related topics. You'd probably want a virtual environment,
and a setup.py, etc.
So for the quick and dirty, two ways to modify the python import path:
set the PYTHONPATH= environment variable to include the parent directory of your packages
in findInfoSystem before importing formatOutput.printChecked, add this:
import sys
sys.path.append("..")
i found something very weird. I'm not import direct in module file but import to init.py file of package and then import to module file from packages name and it work. it like that:
from __init__.py of systemInfo package:
import sys,os
from formatOutput import prettyAnnounce,prettyParseData
current_directory = os.getcwd()
# sys.path.insert(1, current_directory+"/formatOutput/")
# sys.path.insert(1,current_directory+"/")
then in findInfoSystem.py module, im imported again like this:
from systemInfo import current_directory, prettyAnnounce as prCh , prettyParseData as prPD
yeah, now it's work like a charm. :))) i even not sure how

Where is this Python3 module layout and import method defined?

With the following module layout:
/path/in/pythonpath/
mymodule/
__init__.py
_mymodule-cpython-37m-darwin.so
In Python 2.7, __init__.py could import the .so as follows:
import _mymodule
But in Python 3.7, I found that _mymodule was not found unless I added the mymodule directory to the PYTHONPATH:
PYTHONPATH=/path/in/pythonpath:/path/in/pythonpath/mymodule
I found this approach used in the typed_ast module installed on the system:
...site-packages/typed_ast/
__init__.py
ast3.py
_ast3.cpython-37m-darwin.so
Where ast3.py imported _ast3 as follows:
from typed_ast import _ast3
from typed_ast._ast3 import *
Following this model with mymodule also works without having to modify the PYTHONPATH:
from mymodule import _mymodule
from mymodule._mymodule import *
My question is around what changed and what is the right thing to do for "mymodule" - and where is this defined? I didn't find this explicitly covered in the PEPs or the Python Documentation. I suspect it is implicit in some of the module docs and I missed it.
The original problem statement was incorrect.
In Python 2.7, the module I was working with had a Python27 directory with _mymodule.so in it:
Python27/
_mymodule.so
And it explicitly added the Python27 to the path. So it is no surprise that Python 3 couldn't find the module as the subdirectory was eliminated and the versioning naming scheme was applied to the .so file.
As to the two import statements. Both are not required, unless you want to be able to access _mymodule functions directly as well as prefixed by _mymodule. This is typically done for legacy reasons.
The answer to my question is: you asked the wrong question because you mischaracterized the problem and, what you want is:
from mymodule._mymodule import *

Python nested import 'ModuleNotFound' error

I have the following folder structure:
high_level.py (top level)
low_level (directory)
low_level_script.py (within 'low_level')
config.py (within 'low_level')
And the following code:
high_level.py
from low_level import low_level_script
low_level_script.test_fun()
low_level_script.py
import config
def test_fun():
return config.A
config.py
A = 1
If I do the following, at the top level, I get an import error.
import high_level
ModuleNotFoundError: No module named 'config'
Why is this, and what is the best way of making the script.py able to import config.py, in a way that makes script.py importable from its own directory, and the directory above?
The reason why I'm interested in this is because I want to have a pytest tests in test_high_level.py, and test_low_level.py pytest script that import the high-level and low-level scripts.
I'm using Python 3.7. I see another question asking something similar here, but there doesn't seem to be a concrete suggestion, except reading all of this documentation.
Nested Python module imports
https://docs.python.org/3/reference/import.html
This question also seems similar - it recommends using libname, but I'm not completely sure how this addresses the problem.
Python: ModuleNotFound Error
To use relative imports you should precede the module name with a dot.
import .config
or use absolute imports instead
from low_level import config

ImportError: No module named UDPInterface

I am trying to build a Python app and ran into this import error:
ImportError: No module named UDPInterface
I am new to Python and hence I am unsure what dependency should I pip install. Tried searching on the internet but couldn't find anything useful. Any help would be highly appreciated.
from Functions import *
from ScriptComms import *
from ScriptForms import *
from IPInterface import TCP_APIInterface
from multiprocessing.synchronize import Lock
import UDPInterface
Given your imports it looks like it can't find the local python file UDPInterface.py. I believe it to be local given I cannot find any python module with that name. In the same directory level of your script there should be a file named UDPInterface.py. Could also be a simple typo in the filename or import. That or UDPInterface is in a sub/another directory and the import should be adjusted as so.
PS. You should avoid using * imports in Python (generally other languages also) as it can create namespace collisions (two modules with functions/classes/variables of the same name). Try from my_module import func1, func2 as it is more explicit and makes it easier to track down the source of the function/class/variable

Python - fails importing package

I have trouble importing package.
My file structure is like this:
filelib/
__init__.py
converters/
__init__.py
cmp2locus.py
modelmaker/
__init__.py
command_file.py
In module command_file.py I have a class named CommandFile which i want to call in the cmp2locus.py module.
I have tried the following in cmp2locus.py module:
import filelib.modelmaker.command_file
import modelmaker.command_file
from filelib.modelmaker.command_file import CommandFile
All these options return ImportError: No modules named ...
Appreciate any hint on solving this. I do not understand why this import does not work.
To perform these imports you have 3 options, I'll list them in the order I'd prefer. (For all of these options I will be assuming python 3)
Relative imports
Your file structure looks like a proper package file structure so this should work however anyone else trying this option should note that it requires you to be in a package; this won't work for some random script.
You'll also need to run the script doing the importing from outside the package, for example by importing it and running it from there rather than just running the cmp2locus.py script directly
Then you'll need to change your imports to be relative by using ..
So:
import filelib.modelmaker.command_file
becomes
from ..modelmaker import command_file
The .. refers to the parent folder (like the hidden file in file systems).
Also note you have to use the from import syntax because names starting with .. aren't valid identifiers in python. However you can of course import it as whatever you'd like using from import as.
See also the PEP
Absolute imports
If you place your package in site-packages (the directories returned by site.getsitepackages()) you will be able to use the format of imports that you were trying to use in the question. Note that this requires any users of your package to install it there too so this isn't ideal (although they probably would, relying on it is bad).
Modifying the python path
As Meera answered you can also directly modify the python path by using sys.
I dislike this option personally as it feels very 'hacky' but I've been told it can be useful as it gives you precise control of what you can import.
To import from another folder, you have to append that path of the folder to sys.path:
import sys
sys.path.append('path/filelib/modelmaker')
import command_file

Categories

Resources