简体   繁体   中英

Importing everything from parent folder as a module?

My project is structured like this right now:

Game
|__init__.py
|Breakout.py
|Ball.py
|Pad.py
|Shared -
        |__init__.py
        |GameConstants.py

The __init__.py file in the Game folder has the following text:

from Game.Ball import Ball
from Game.Pad import Pad

What I am trying to do, is importing this parent module in the Breakout class. Doing something like this works in the PyCharm IDE:

from Game import *

But it does not work in another IDE, like Visual Studio Code.

Is there an easy solution to do this? (Running python 3.6)

You can use the sys module to add the directory 'one up' to your path. Then you can access the files in your current directory.

__init__.py:

import sys
sys.path.append('..')
from Game.Ball import Ball
from Game.Pad import Pad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM