Created
May 20, 2010 19:31
-
-
Save hltbra/407979 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# estrutura de pastas: | |
# foo | |
# | abc.py | |
# |-- bar | |
# | |-- xyz.py | |
# imagine que o diretório atual é bar e você quer importar abc | |
import sys | |
import os | |
# __file__ é o nome do arquivo .py | |
# os.path.dirname pega o nome do diretório pai de um caminho | |
bar_dir = os.path.abspath(os.path.dirname(__file__)) | |
foo_dir = os.path.dirname(bar_dir) | |
# foi usado insert ao invés de append pra colocar na primeira posição, e não na última | |
# porque se existir algum abc de fora, deve importar o dessa estrutura | |
sys.path.insert(0, foo_dir) | |
import abc | |
# TADA! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment