0

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.oywith:

from .useful import Hash 
2
  • 1
    folder2 doesn't have an __init__.py file to make it a module. You should also wait to ask the question when you're in a position to create a minimal reproducible example and respond to feedback. Commented Oct 16, 2020 at 14:37
  • In my actual project there is a __init__.py file in Folder2. I forgot to add that to my question. Commented Oct 16, 2020 at 14:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

On my Desktop I've got a folder Named Code. In code I've got other Folders named after projects like "findPath". In "findPath" there is a file named "findPath.py". In "code" there is a folder named "useful" which contains useful projects stores in subfolders I've created like a RSA implementation. Now I want to get the "RSA" class from the "RSA.py" file in the "RSA" folder located in the "useful" folder in code. Ill add this to my question.
I found a similar problem and solution here: codeolives.com/2020/01/10/… I'd recommend to store useful functions that are frequently used in one file in a neighbour folder for other scripts. For example "Code" contains "utils" with file "utils.py" where you store all useful functions. In "Code", make other folders like "findPath". From scripts in "findPath", you can import functions and classes like I was showing.
Thanks MD98. Works now with the code from the link.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.