0

Anaconda allows users to have different versions of Python and R installed at the same time. These versions are managed in environments, which can be activated and deactivated according to the user's preference.

I would like to specify which version of Python or R to use when I execute a script on the command line (regardless of which environment is active). This could look like

python -version 3.7 myPy3Script.py
python -version 2.7 myPy2Script.py

Here, the first command would execute my script in Python 3, whereas the second line would execute my second script in Python 2.

Can I actually do that in practice? If so, how?

Of course, I could just specify the path to the respective python executable. However, would the libraries be loaded correctly in this case? (See this issue). Also, would there be a way to avoid typing in lengthy paths?

Samufi
  • 2,465
  • 3
  • 19
  • 43
  • Do you specifically want to be able to change python versions from the command line on the fly? Or do you just want to associate certain files with certain python versions? Shebang seems like a valid option depending on your operating system. – busybear Apr 17 '19 at 01:47
  • I would like an on-the-fly solution. A by-file solution would also be interesting, but is not my primary goal. I am interested in solutions that work on Windows (first priority) and Linux (second priority). – Samufi Apr 17 '19 at 01:50

1 Answers1

1

Probably the easiest, and Anaconda's intended way, to specify which Python version to use is to activate your environment before each call. So something like:

conda activate <Python 3 env> && python myPy3Script.py
conda activate <Python 2 env> && python myPy2Script.py

Alternatively, if you are using Linux (or other Unix system), you can define a shebang at the beginning of your file to specify the program to execute the script with. So for instance, your myPy3Script.py might look like this:

#!/path/to/python3

<Code within your script>
...
busybear
  • 10,194
  • 1
  • 25
  • 42
  • Thanks for the Shebang suggestion. However, I would be specifically interested in a solution that works *without* needing to activate an environment - also, because environments get ativated for a specific shell only. So I would need a command that does both activating the desired evironment and exececuting the script in the same line. – Samufi Apr 17 '19 at 03:16
  • I just changed the syntax to fit Windows and Linux, but _technically_ they would be one liners. I am a little confused though: you say you want something without needing to activate an environment, but then go on to say you need something that both activates an environment and executes a script. – busybear Apr 17 '19 at 13:27
  • Indeed this was confusing, I am sorry. You are right that this are technically one-liners. I was hoping for some solution that would allow me to put my R environment in the PATH without messing with my usual python environment. I may think about this a little further... – Samufi Apr 17 '19 at 18:07