[英]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
. 我有一个文件夹结构,让我们说/home/ubuntu/pf/basic/transmitter
,我有一个文件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
. 它有一个可变encoder
,我想从文件estimator.py
中的文件夹/home/ubuntu/pf/basic/reciever/helper/decoder
estimator.py
是在main.py
导入的。 So i tried using previous discussion to add the following in estimator.py
所以我尝试使用前面的讨论在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 ? 有另一种在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: 编辑:显然你添加了错误的路径,如果要导入encode.py,你应该添加其路径而不是其他脚本,即你应该将代码更改为:
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: 你也有其他选择,你可以使用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 您也可以将encode.py放在与estimator.py相同的文件夹中
or you can add '/home/ubuntu/pf/basic/transmitter' to your os environmental variables 或者您可以将'/ home / ubuntu / pf / basic / transmitter'添加到您的os环境变量中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.