Import module from another folder

Asked

Viewed 1,612 times

5

I want to import a module that is found in a folder that is sister to the folder where I have the script, inside which I want to import the other script, ie:

logic\ 
    board.py`
    mlogic.py
    player.py
gui\
    mgui.py

logic and gui are the sister folders, and I am currently within mgui.py and would like to import the class Board of the script board.py. With the PyCharm, i make this import from logic.board import Board, and it works, but not with the IDLE, why? How can I solve this? It seems strange that one works and the other does not...

2 answers

1

you can create a "init.py" import everything inside the folder, but you tbm can do from import "folder" file name": importando arquivo na pasta on the issue of init the use and so on: usando init

  • Remove the images and post the code used. After you type select the code and press Ctrl+K to make it look right, indented, etc.

  • on Ctrl +k n shows the file created inside the print folder and to show it

0

First, in both folders you must have a file __init__.py, after this, some module must be in a previous directory to each other, or you import the two into a python in the root directory of both!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.