What is the difference between activating an anaconda environment and running its python executable directly?

坚强是说给别人听的谎言 提交于 2021-01-28 05:54:37

问题


I have setup multiple python environment using Anaconda.

Usually, to run a script "manually", I would open a command line and then type:

activate my-env
python path/to/my/script.py

Fine.

Now I am trying to run a script automatically using a scheduler and I was wondering what the difference was between

  1. Writing a batch which activates the environment and the executes the script (like in the snippet above)

  2. Calling directly the python executable from the environment (within the envs/my-enjv/ directory) like below:

    /path/to/envs/my-env/python.exe path/to/my/script.py

Both seem to work fine. Is there any difference?


回答1:


I don't claim to be an expert but here's my 2 cents.

For small scripts, no, there isn't a difference.

You should notice a difference when calling external modules / packages. conda activate alters the system path to change how the command shell searches for the appropriate capabilities.

If you supply a full path to an interpreter and the full path to an isolated script, then the shell doesn't need to do a lookup as this has priority over the path. This means you could be in a situation where the interpreter can see the script but cannot see dependencies.

If you follow the conda activate process, and the environment is correctly packaged, then the shell will be able to trace any additional resources.

EDIT: The idea behind this is portability. If an admin has been careful in setting up a system, then scripts should have the appropriate visibility - i.e. see everything in it's environment plus everything in the main system installation.

It's possible to full-path every call to an interpreter and a script or package location, but then what happens when you need to move it to another machine? You would need to spend a lot of time setting everything up exactly as it was before. On the other hand, you can follow the package process and the system path will trace everything for you.




回答2:


Simply checkout the PATH variable in your environment. After conda activation it has been extended by

\Anaconda3;
\Anaconda3\Library\mingw-w64\bin;
\Anaconda3\Library\usr\bin;
\Anaconda3\Library\bin;
\Anaconda3\Scripts;
\Anaconda3\bin;

This doesn't make much of a difference, if you are just using the standard library in your code. However, if you rely on external packages like pandas, it's a prerequisite so that the modules can be found.



来源:https://stackoverflow.com/questions/50575330/what-is-the-difference-between-activating-an-anaconda-environment-and-running-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!