I got a strange issue and have no idea about the reason.
As you can see from the picture, folder fuzzier and parser are under the same parent folder, and both of them have the file __init__.py (both empty because I am not using from xxx import *, and code is based on Python 3.6).
And in another module (under the same parent folder with fuzzier and parser), there is a file doing some import like this:
import fuzzier.jison
import parser.annoying_char
The first line is good, but the second line is with an error ModuleNotFoundError: No module named 'parser.annoying_char'; 'parser' is not a package
I wasted hours on this and wish someone can help with this, Thanks!
parser is an in-built library in Python.
Python is trying to find annoying_char inside that library instead of your module.
You should use some other name.
Source - https://docs.python.org/2/library/parser.html
Related
I'm using VSCode and getting warning "search is not accessed in Pylance" when trying to import my own module, search is the name of the module I need to import to my main program
1- I do have __init__.py file on module folder
2- Tried changing interpreter
Importing mymodule as below:
from src import search
folder structure:
Could the reason of this be that 'src' is being treated as a module instead of a folder?
Is there an alternative way to import my modules?
Thanks
The error "search" is not accessed means you are not using search,
If you use search below then this error will go away.
I have searched this question over stackoverflow but can't find an answer that fixes this. I am trying to learn how to do proper imports with python.
I am using Python 3.8.2 and I have the following simple directory setup.
main_folder\
folder1\
myclass.py
folder2\
testclass2.py
testclass1.py
Both testclass1.py and testclass2.py have this inside:
from folder1.myclass import Myclass
This works fine for testclass1.py, but when I run in testclass2.py it gives me an error.
ModuleNotFoundError: No module named 'folder1'
Even though I had read Python no longer requires this, I inserted an __init__.py file into folder1. This generated the same error. I then tried following directions for the init file in this article but there was no improvement. I also tried using relative paths versus absolute paths for import, but no success.
Help is much appreciated.
From testclass2.py the import should be like this:
from ..folder1.myclass import Myclass
I am trying to import python files which have been developed. Initially, when all files were in the same directory then I there was no problems with them. However, I decided to split projects to dedicated directories. After that, I am unable to import files anymore.
My file structure is similar to the following:
dirFoo\
mainFoo1.py
dirFoo\
mainFoo2.py
dirFooCommon\
commonFoo.py
commonFoo2.py
Initially, I was trying to change the import path of my mainFoo1.py and do:
from dirFooCommon import commonFoo. However, that approach gave me the following error:
ModuleNotFoundError: No module named 'common'.
Apart from that, I tried to use imp.load_source which seems that is working. Unfortunately, this is generating so additional problems. For example what if some of the commonFoo.py libraries must include others? In this case, each of them needs to use an absolute project path, which will be a problem when I will try to use common libraries in any other place.
Do you have an idea what should I do to include my common libraries to my main projects?
You can use this folder structure that will allow you to call each package and module properly.
dirFoo\ <=== This is a package know
__init__.py
mainFoo1.py
dirFoo2\ <==== Change the name or you will have namespace issue
__init__.py
mainFoo2.py
dirFooCommon\ <=== This is a package know
__init__.py
commonFoo.py
commonFoo2.py
So in mainFoo1.pyyou can call commonFoo.pylike this
import sys
sys.path.append("/path/to/dirFooCommon")
from dirFooCommon import commonFoo
To replace sys.path.appendyou can also add the folder in your PYTHONPATH.
I am struggling to get module importing to work in AWS lambda using Python 3.
My file structure looks like this:
package:
stage1.py
__init__.py
util:
helper1.py
__init__.py
helper1.py is a simple util class:
def foo():
print("yes")
Within stage1.py I have the following lines that cause Lambda to throw an error when it is starting:
from util.helper1 import foo
foo()
Unable to import module 'package/stage1': No module named 'util'
Both __init__.py files are empty.
Sadly, I see that this works if I invoke the script locally. Frustrating is an understatement!
Thanks to some of the links sent above and my own (and necessary) research into how imports are handled in python, I figured out the issue regarding unavailable modules.
How I debugged my app in Lambda:
I attached this line of code to the top of the file
print("Name is ({})".format(__name__))
This gave me an output that could help me understand an make an educated decision on how to import the files in the util module. I saw an output for the stage1.py file was packager/stage1. This made the import code modifications easy to make.
I changed the imports in the stage1.py file to (using absolute path imports -- pep recommendation):
from packager.util.helper1 import foo
For whatever subjective reason, this link helped me understand the process the most.
I avoided this issue using a leading dot in the path like this
# package.py
from .util.helper1 import foo
I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.