Δημοσιεύτηκε: 28 Ιαν 2012, 19:36
από sokoban4ever
@Ilias95
Βασικά το sys.path.append('..') νομίζω ότι δεν είναι και τόσο "άσχημη" λύση για ένα-δυό imports ...

αλλα για να μας λυθούν οι απορίες ας συγκρίνουμε τι εννοεί ο καθένας..... ;)


Λοιπόν (από ότι κατάλαβα ότι έλεγες έχουμε και λέμε)

0) έχουμε αυτη την δομή φακέλων
Spoiler: show
Κώδικας: Επιλογή όλων
sokoban@Laptop:~/Desktop/TESTS$ tree
.
└── app
├── __init__.py
├── run_tests.py
└── tests
├── __init__.py
└── test_something.py

2 directories, 4 files


και αν συγκριμένα ισχύουν
1) τα __init__.py είναι άδεια
2) εκτελείς το run_test.py μέσα από το φάκελο app , δηλαδή το sys.path[0] = /home/username/some/folder/app
3) το run_test.py κάνει from tests import test_somthing
Spoiler: show
Κώδικας: Επιλογή όλων

from tests import test_something
import sys

print " ' from tests import test_something' --> OK "

test_something.TFUNC()

print " ' Call test_something.TFUNC ' --> OK"

print "\nsys.path[0] --> " , sys.path[0] ,"\n"

4)και το test_something.py έχει κάποια συνάρτηση στο namespace του

Spoiler: show
Κώδικας: Επιλογή όλων
__name__ = "test_something"

def TFUNC():
print "Succesfully executed test_something.FUNC from module ", __name__


5) εκτελώ από το φάκελο app το run_tests.py
και το αποτέλεσμα είναι
Spoiler: show
Κώδικας: Επιλογή όλων
' from tests import test_something' --> OK
Succesfully executed test_something.FUNC from module test_something
' Call test_something.TFUNC ' --> OK

sys.path[0] --> /home/sokoban/Desktop/TESTS/app

Μήπως δεν έχεις κάνει την __name__ σε test_something ; :problem:
αν δεν το έχεις κάνει
τότε τι ακριβώς έχεις κάνει;