Dowemo

Some tips for importing import in python

In a. Py file in directory a, the b. Py file of import directory b:


sys.path.append('path to b.py')


import b






Sometimes you need to use the same directory in the same directory as b. Py, for example.


#b.py中


data.csv:pd.read_csv('./data.csv')






The relative path '. 'is the directory where the py is located, or the directory where the script is invoked. To get the absolute path to the b. Py, you can use.


#b.py中


b_path=os.path.split(os.path.realpath(__file__))[0]






It's more complex to include a. Py in the import parent directory involving the previous relationship. In general, the cd to the lowest layer calls the script


python3 -m./child_dir/child.py









Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs