6

This is the situation. I'm using Python 3.6

I currently have the next folder organization:

/MainProject
 __init__.py
 /Folder1
 pyscript1.py
 pyscript2.py
 __init__.py
 /Folder2
 pyscript3.py
 __init__.py

So, I'm trying to get a function that exists in pyscript1.py from pyscript3. I've also added a init.py at every level. Inside pyscript3.py I tried the following:

from . import Folder1

Giving the error:

ImportError: cannot import name 'Folder1'

Also I tried:

from .Utils import script1

Giving the error:

ModuleNotFoundError: No module named '__main__.Utils'; '__main__' is not a 
package

I know that I can solve it using sys and os in the following manner:

sys.path.append(os.path.realpath('../..'))

But I wanted to know if this is possible without using sys.

asked Jul 11, 2018 at 23:19
0

3 Answers 3

9

Note that Folder1 is a directory, the .py scripts are your modules.

In pyscript3 you should be able to go:

from Folder1 import pyscript1

Then you can access a method with name methodname like:

pyscript1.methodname()

Otherwise you can import the method directly like:

from Folder1.pyscript1 import methodname

and use it like:

methodname()

EDIT:

For your program to see Folder1 and Folder2, you need to run your program from the MainProject folder.

Either move pyscript3 to your MainFolder or write another script, let's call it main.py, and call the necessary code to instantiate the class/call the function you want in pyscript3.py.

To summarize, you always want to run the entry module from the base folder of your project.

answered Jul 12, 2018 at 0:11
Sign up to request clarification or add additional context in comments.

8 Comments

That's the issue I commented, I Get ModuleNotFoundError: No module named 'Folder1'
You can't import Folder1, but you can import a script from Folder1. Can you maybe expand on your MainProject folder? Where is the entry-point?
I'm trying to get from pyscript3 to pyscript1, the entry point would be pyscript3.
Could you show your main.py or equivalent file? Presumably it's in the MainProject folder?
Also, how are you running your program? Simply python main.py?
|
0

If still not working, check the PYTHONPATH Environment Variable you defined for your project. Sometimes one defines a PYTHONPATH just for the root folder of the project but wants to import modules from a folder that is inside the PYTHONPATH that is defined.

Options to solve this:

  1. Import the module adding the parent folder name, for example: import MainProject.Folder1.pyscript1 instead of import Folder1.pyscript1.
  2. Add another PYTHONPATH for the subfolder in which you have the modules to import.
answered Jul 6, 2023 at 15:19

Comments

-1

You could use from ..Folder1 import pyscript1 In pyscript3.py But you would have to load pyscript3 from the parent module or in your case MainProject.

answered Jul 11, 2018 at 23:31

1 Comment

That is two levels above, and doesn't work, and I can't load pyscript3 from the parent module, also if I use ..Folder1, that gives: ValueError: attempted relative import beyond top-level package

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.