2017-03-05 8 views
0

Ich habe eine virtuelle Umgebung my_env in der ich Anaconda installiert. Wenn ich tippeImportfehler: Anaconda numpy (numpy und Anaconda bereits installiert, virtualenv)

which python 

ich:

/user/pkgs/anaconda2/envs/my_env/bin/python 

Ich habe hier keine Fehler importieren numpy:

(my_env) [email protected]:~/my_dir$ python 
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
Anaconda is brought to you by Continuum Analytics. 
Please check out: http://continuum.io/thanks and https://anaconda.org 
>>> import numpy as np 
>>> 

Aber wenn ich sage, 'Import numpy als np' in einem Python-Programm und Führen Sie das aus einem Shell-Skript, ich bekomme:

(my_env) [email protected]:~/mydir$ ./program.sh 
Traceback (most recent call last): 
    File "../python_program.py", line 3, in <module> 
    import numpy as np 
ImportError: No module named numpy 

Wie kann ich das beheben?

EDIT: Ich wurde gefragt, was in program.sh ist. Die kurze Antwort ist, dass ich verschiedene Parameter in einer Schleife laufen lasse. Die lange Antwort ist:

#/bin/bash 

i=0 
while read a1 b1 c1 d1 e1 f1 g1 h1 i1 
    do 
    i=$(($i+1)) 
    mkdir RUN_EXP$i 
    cp $a1 RUN_EXP$i 
    cd RUN_EXP$i 
    ../python_program.py --filename $a1 --reps $b1 --pop $c1 --susc $d1 --exp_trans $e1 --inf_period $f1\ $g1 --eps $h1\ $i1 
    cd .. 
    done < readparas.txt 

Die Datei readparas.txt hat Zeilen enthält Dateiname, Wiederholungen, pop, Susc, exp_trans, inf_period und eps wie folgt aus:

run_1.txt 50 162 0.30 0.1 5 9 0.1 0.25 
run_1.txt 50 162 0.30 0.3 5 9 0.1 0.25 
... 
+0

Was ist in 'program.sh'? – DyZ

+0

@DYZ siehe oben Bearbeiten. – StatsSorceress

Antwort

2

Ihr Shell-Skript nicht Sorge dafür, dass ein virtualenv aktiv ist (es beginnt in einer sauberen Umgebung). Siehe

Statt ../python_program.py müssen Sie den vollständigen Pfad für ausführbare Dateien

export PYTHON_ENV=/user/pkgs/anaconda2/envs/my_env 

$PYTHON_ENV/bin/python ../python_program.py --filename $a1 ... 

haben oder Sie können diese von python_program.py

#!/usr/bin/env python 

an die Spitze anhängen: The importance of env (and how it works with virtualenv)

+0

Warum ist der vollständige ausführbare Pfad erforderlich? Es war nicht mit einem ähnlichen Programm, das ich vorher lief. – StatsSorceress

+0

Da Sie ein 'virtualenv' verwenden, in dem' numpy' installiert ist. Ihr anderes Programm verwendete Ihr System Python und hatte alle verfügbaren Module. –

+0

Außer das Problem passiert auch außerhalb der Virtualenv. – StatsSorceress