问题
I need to run jenkins build using a specific conda environment on our jenkins server (running on Windows). I thought it would be as easy as running:
activate myenv
python test_env.py
but this seems to cause the build to exit instead, before the script even starts. Here is the jenkins console log:
activate myenv
Activating environment "myenv"...
Finished: SUCCESS
If I remove the activate line, the python script executes fine.
FYI, the script I am running:
import os
f = open('env.txt','w')
for k, v in os.environ.iteritems():
print k, v
f.write('%s\t%s\n' % (k,v))
f.close()
Does anybody know what is going on? Should I directly call the relevant python executable instead?
回答1:
Use call activate myenv
to prevent activate from ending the current shell when it is finished. See https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/call.mspx?mfr=true.
回答2:
Bradley led me in the right direction and I found the solution...
I needed a windows equivalent for the Unix "source", and "call" does the job, as detailed in this other answer.
batch equivalent of "source" on windows: how to run a python script from a virtualenv
I hope someone will find this helpful in the future!
回答3:
I think the activate script on Windows starts a new subshell which means the current shell exists immediately. Can you try manually setting the ENV variables like PATH and such instead of using activate?
来源:https://stackoverflow.com/questions/25546774/how-to-activate-conda-environment-in-jenkins-build