0
  1. I am trying to understand PYTHONPATH related to my project.

My project is in the dir $HOME/Programs/medusa-2.0, and my source files are in $HOME/Programs/medusa-2.0/medusa.

I have set my PYTHONPATH in the .bashrc like this:

export MEDUSA_HOME=$HOME/Programs/medusa-2.0
export PYTHONPATH=${MEDUSA_HOME}/medusa:${PYTHONPATH}

When I try to import a class, from system import error_handler, hello, I get errors saying that it cannot find the function execute_command. I don' t understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in 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

I have tried to launch a python file with python -v, and I've got this error:

# /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
  1. If I launch my virtualenv for my project, shouldn't the PYTHONPATH be defined inside the virtualenv?
xeon123
  • 819
  • 1
  • 10
  • 25
  • 1
    If you use the `-v`erbose flag when starting Python it will explain a lot about imports – Wayne Werner Jan 28 '16 at 19:06
  • In my case, it didn't explain me much. You can check what I've added in the original post. – xeon123 Jan 28 '16 at 19:10
  • The error part isn't what you should be looking at - watch all of the messages that go by. You'll see where it's matching files from. Have *you* written a module named `medusasystem.py` or `.pyc` in your path anywhere? – Wayne Werner Jan 28 '16 at 19:17
  • First, all the log seems ok, except the error that I get. Second, I am not understanding your question about the `.py` or `pyc`. I have written `py` files, and it is the python engine (I don't know the name) that converts the files into `pyc`. – xeon123 Jan 28 '16 at 19:28
  • The log will tell you where it's importing `medusasystem` from. Presumably that's in `/home/ubuntu/Programs/medusa-2.0/medusa/medusa.py`. But it might not be. If *you* created `medusasystem.py` or it exists anywhere else on `sys.path` then it's probably no the one medusa expects. Either way you can go to that file and check to see if there's an `execute_command` function in that particular file. – Wayne Werner Jan 28 '16 at 19:53

2 Answers2

0
  1. With the information presented, I think you are just importing it from the wrong place: "I don't understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in medusasettings?" but in the trace(s) there are from settings import medusa_settings and from medusasystem import execute_command. Verify that execute_command is in medusasystem.

  2. virtualenv is not going to change PYTHONPATH, so it will be the same after activation (unless of course, you do something like what is in the next sentence). If your question is about setting it when using the virtualenv, see this answer: How do you set your pythonpath in an already-created virtualenv?. By doing it in .bashrc, you are defining it when you open the shell rather than attaching it to the virtualenv script(s).

Community
  • 1
  • 1
Jmills
  • 2,414
  • 2
  • 19
  • 22
0

The problom was with circular import issue. I replaced:

from medusasystem import execute_command

with

import medusasystem
execute_command = medusasystem.execute_command

and it worked.

xeon123
  • 819
  • 1
  • 10
  • 25