I am writing a simple package in python. In the root directory, my structure currently is,
I want to import all functions from main_code.py in test_main_code.py. To achieve this, I did
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '\..' + '\my_package')
from main_code import *
This runs smoothly on my system. But when I push the code to github and tests are run on travis-ci, all tests fail and return NameError. It says given function name not defined. How do I make sure that tests run on travis-ci as well ?
Paths could be different if you run the test somewhere else, so I wouldn't trust any import related to a particular location. In any case, I always try to run the tests from the root of the project .
Then, you can simply try:
from my_package.main_code import *
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.