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
Related
am beginner in python and working on multi folder structure and struggling to refer the files using relative path. tried in different ways to refer the files in other folders but ended up in error "attempted relative import with no known parent package". Can somebody explain how the relative paths will work in python?
here is the example what am trying to achieve..
folder structure
Main
source
Function1
inputs
1.py
2.py
uts
test_1.py
code_1.py
code_2.py
Now inside test_1.py am trying to refer code_1 using relative path like
from ..code_1 import Code
from .code_1 import Code
but, ended up in error..
some where I found that we need to have init.py in each folder to use relative path and I created empty init.py in each folder but, no use..
I may get the answer but more importantly need to know how this relative path works in python.
Thanks in advance
I've also had issues with relative imports in Python and thus I've created an experimental, new import library: ultraimport
It gives you more control over your imports and lets you do file system based imports.
You could then write in your test_1.py:
import ultraimport
Code = ultraimport('__dir__/code_1.py', 'Code')
This would import code_1.py from the same directory as test_1.py.
This will always work, no matter how you run your code.
There are 10+ SO posts about this already, none of the answers works for me and I still haven't seen an example of someone importing something from a sibling directory.
src
__init__.py
test.py
package1
__init__.py
module1.py
package2
__init__.py
module2.py
(_init_.py should not be necessary on python versions greater than 3.3 but I still have them there as they make no difference)
in test.py I have
import package1.module2
and it works fine however the problem is when I want to import something from package2 to package1, and vice versa. I have tried different import methods in module2.py and I receive these different error messages:
import src.package1.module1.py
with the error:
ModuleNotFoundError: No module named 'src'
and
from .. import package1
with the error:
ImportError: attempted relative import with no known parent package
The top answer here: How do I import a Python script from a sibling directory? also give me the exact error message as I showed above.
The answers here: How to import a Python module from a sibling folder? changes nothing.
Am I missing something or should it not be possible to import stuff between different folders/packages? Do I need the "sys.path hack"?
I gave almost all of the solutions given for similar questions a try, but none of them worked for me!
However, after banging my head to the wall, I found out that this solution does work by just removing one dot:
import sys
sys.path.append('..')
Just remove one dot from string within append method, i.e.:
sys.path.append('.')
or your can use:
sys.path.insert(0, '.')
Then you can import any module from a sibling folder.
I tried this using python 3.9.13 and it worked well.
I just spent several hours struggling with this same problem, trying various solutions from answers to the linked questions, and none of them worked. It was only when I cleared out the cruft from my various failed attempts and tried the stupidly simple solution from package1 import module1 (NB no dots, i.e. not a relative import) that it worked!
Note that your IDE may complain about this (PyCharm does in my current main project, but doesn't in a dummy project I created to test this solution separately). Nonetheless, when you run the code it should work (tested with Python 3.10 on Windows 11).
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 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
I am currently working in the following project, and running into some difficulty with imports. I previously came from a Ruby background before Python, so I suspect I'm just missing something.
-src
--project
---actions
----some .py files
---config
----some .py files
---db
----some .py files
-tests
--some .py files
-run.py
Some of the actions I'd like to do are:
import src/project/config/file.py from run.py
import between second level folder in project (ie. file in actions imports something from config)
import any file into a test
Would anyone have any advice on how to accomplish this?
So it's quite easy to reference something going deeper into the directory . For example, from run.py, you would be able to use something such as:
from src.project.config.file import foo
It's easy to access something in the same directory as well, say you're in src/file1.py trying to access src/file2.py, this would just be:
from file2 import foo
This works too. However if you were trying to import upward say from file.py to run.py and tried running something like this:
from ... import run.foo
You would get the following error:
ValueError: attempted relative import beyond top-level package
The problem is that Python is assuming that your top-level package is wherever the file being run is located. That's why you can import everything on the same level and deeper but can't get at anything above.
An easy hack to get past this is to just append the path to your file to your sys.path so:
import sys
sys.append('path/to/run')
from run import foo
So that's the answer to you question.
Based on the way your project is setup though it seems like you want to make a module and that's a bit more hands on, I suggest reading the documentation it's pretty good.