For anyone interested: The easiest solution was
import sys
sys.path.append('../')
from Folder2.Folder2-1.File2 import Foo
I've got this structure:
Program:
Folder1:
__init__.py
File1.py
Folder2:
__init__.py
Folder2-1
__init__.py
File2.py
File1:
from .Folder2.Folder2-1.File2 import Foo
Foo().hello()
File2:
class Foo:
def hello(self):
print("Hello!")
I want to import the Foo
class located in File2.py
into File1.py
.
I've tried:
from .Folder2.Folder2-1.File2 import Foo
That gives
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 1, in <module>
ImportError: attempted relative import with no known parent package
As you can see I'm on my phone because I'm not home right now. It should be the same as Linux tho. I added the __init__.py files because in other questions they were mentioned.
My actual structure for MD98s answer:
Desktop:
Code:
findPath:
__init__.py
findPath.py
testFindPath.py #empty
useful:
__init__.py
RSA:
__init__.py
RSA.py
RSAresources.py
I would like to import the RSA
class from RSA.py
into findPath.py
So, if I had:
Desktop:
Code:
findPath:
__init__.py
findPath.py
testFindPath.py #empty
__init__.py
useful.py
useful.py:
class RSA:
def encript(self,msg):
return 123456778
class Hash:
def hash(self):
return 1234456778
I would import Hash
into findPath.oy
with:
from .useful import Hash
I am confused by your folder structure, but normally you can import classes or methods like this, assuming the script you're writing is inside your project directory:
from foldername.filename import Foo
or
from folder name.foldername2.filename import Foo
I hope this helps you. Otherwise please be a little more precise about your folder structure.