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
Related
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
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
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
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
I know a similar question has been answered ad nauseam in these pages, but I've read all the answers and cannot find a solution that makes my nuanced application work. The long and short:
I would like to expose an external module (that only exists in a relative path, and I cannot update the PATH variable) to my working module. In each case, I recognize that I could import the module using a sys.path.append()/import. However, I would like to import the module in my __init__.py and expose it to the module there (so I only have to adjust the path once, theoretically). Everything I have read indicates that once I have exposed the module via the import in __init__.py, I should be able to access it via a call like my_module.imported_module. However, that does not seem to work. The basic idea is to share a set of functions across multiple modules that should also all be independent. Example below:
(base module):
__init__.py:
import sys
sys.path.append('../') # I know this is not ideal, but for simplicity
import core
myfile:
import base
print dir(core)
NameError: name 'core' is not defined
Alternatively:
myfile:
import base
print dir(base.core)
AttributeError: 'module' object has no attribute 'core'
And last:
myfile:
import base.core
print dir(base.core)
ImportError: No module named core
**last, last:
And last:
myfile:
import core
print dir(core)
ImportError: No module named core
Any ideas?
you don't really have a choice here, if it's a relative path in a directory outside of your main module, you're going to have to manipulate the path. You should change your sys.path manipulation to work with __file__ instead so you aren't bound to current working directly, i.e.:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__))
However, if the other package shares a package with your file you could use a relative import:
parent/
__init__.py
core.py
base/
__init__.py
myfile.py
You can also do this path manipulation in your .pythonrc or with your ipython settings.