我正在尝试了解与我的项目相关的PYTHONPATH
。
我的项目位于目录$HOME/Programs/medusa-2.0
,我的源文件位于$HOME/Programs/medusa-2.0/medusa
。
我在.bashrc
中设置了我的PYTHONPATH:
export MEDUSA_HOME=$HOME/Programs/medusa-2.0
export PYTHONPATH=${MEDUSA_HOME}/medusa:${PYTHONPATH}
当我尝试导入一个类from system import error_handler, hello
时,我收到错误,说它无法找到函数execute_command
。我不喜欢'明白为什么我会收到这个错误?是因为我在导入中进行循环,因为execute_command
位于medusasettings
?
ImportError Traceback (most recent call last)
<ipython-input-2-7f959e81c735> in <module>()
----> 1 from medusasystem import error_handler, hello
/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py in <module>()
9 from local import lcat
10 import psutil
---> 11 import ranking
12 import settings
13 import simplejson as json
/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py in <module>()
7 import cache
8 from decors import make_verbose
----> 9 from scheduler.predictionranking import get_prediction_metrics
10 from scheduler.randomranking import get_random_metrics
11 from settings import medusa_settings
/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py in <module>()
6
7 from celery import task
----> 8 import hdfs
9 from networkdaemon import read_network_data
10 from numpylinearregression import estimate_job_execution, calculate_linear_regression_numpy
/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py in <module>()
4 from hadoopy._hdfs import _checked_hadoop_fs_command
5 from celery import task
----> 6 from medusasystem import execute_command
7 import settings
8
ImportError: cannot import name execute_command
我尝试使用python -v
启动python文件,但我发现了这个错误:
# /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc matches /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py
import hdfs # precompiled from /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc
Traceback (most recent call last):
File "tests/testHello.py", line 3, in <module>
from medusasystem import error_handler, hello
File "/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py", line 11, in <module>
import ranking
File "/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py", line 9, in <module>
from scheduler.predictionranking import get_prediction_metrics
File "/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py", line 8, in <module>
import hdfs
File "/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py", line 6, in <module>
from medusasystem import execute_command
ImportError: cannot import name execute_command
答案 0 :(得分:0)
根据提供的信息,我认为你只是从错误的地方导入它:“我不明白为什么我会收到这个错误?是因为我在导入中执行循环循环,因为execute_command是在medusasettings?“但是在跟踪中有from settings import medusa_settings
和from medusasystem import execute_command
。验证execute_command是否在medusasystem中。
virtualenv不会改变PYTHONPATH,因此在激活后它会是相同的(当然,除非您执行下一句中的操作)。如果您的问题是关于在使用virtualenv时进行设置,请参阅以下答案:How do you set your pythonpath in an already-created virtualenv?。通过在.bashrc中执行它,您在打开shell时定义它而不是将其附加到virtualenv脚本。
答案 1 :(得分:0)
问题是循环导入问题。我换了:
from medusasystem import execute_command
与
import medusasystem
execute_command = medusasystem.execute_command
并且有效。