简体   繁体   中英

Error in importing a file in different folder and how to access a variable from the file in python?

I have two folders , for my Transmitter and Reciever. I have a folder structure lets say /home/ubuntu/pf/basic/transmitter under which i have a file encode.py . It has a variable encoder which i want to access from the folder /home/ubuntu/pf/basic/reciever/helper/decoder in the file estimator.py The estimator.py is imported in the main.py . So i tried using previous discussion to add the following in estimator.py

import sys
sys.path.insert(0, '/home/ubuntu/pf/basic/reciever/helper/decoder')

It however complains that therew is no module ? IS there another way of importing variables in a file in python ?

And how I can i access the variable ?

Edit: apparently you added the wrong path, if you want to import encode.py you should add its path not the one for other script, ie you should change your code to:

import sys
sys.path.insert(0, '/home/ubuntu/pf/basic/transmitter')
import encode  # or from encode import encoder

also you have some other options, you can use sys.path.append:

import sys
sys.path.append('/home/ubuntu/pf/basic/transmitter')
import encode  # or from encode import encoder

also you can put encode.py in same folder with estimator.py

or you can add '/home/ubuntu/pf/basic/transmitter' to your os environmental variables

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