1

I've made a python script for multiple users and I want to be able to run it by just double clicking the script.

I also have made a conda env for this script and it seems I am unable to activate the environment within my python script (?).

I thought I could write a script which will activate my conda env and then run my python script in command prompt but I'm completely lost and can't find out how to even activate my conda env in command prompt.

Am I heading in the right direction with this - is the best way to activate a conda env and run a python script within one executable script via command prompt?

Sorry if this is a really obvious and/or stupid question, I am very new to all of this!

What I've tried so far:

I have now added conda and python to my path (thank you @Nesha25, I didn't need admin!). I then tried to run my script in the command prompt with: conda run -n Ngon_env python C:\Users\jlp\Desktop\Local_BLAST_scripts\Neisseria\ngon_script.py --live-stream But I get the following error which seems to occur after it tried to use the input function in my python script: EOFError: EOF when reading a line.

I then tried conda run -n Ngon_env --live-stream python C:\Users\jlp\Desktop\Local_BLAST_scripts\Neisseria\ngon_script.py and it doesn't seem to do anything and just gets stuck.

The conda run --no-capture-output flag worked instead, hooray!

Jess
  • 13
  • 1
  • 5
  • What are you using to write and run your script? Vscode? Notepad and cmd? Pycharm? The answer will make a difference. – Nesha25 May 06 '22 at 13:27
  • I wrote and tested the script in Pycharm with my conda env but I don't want other users to need pycharm to run it (if this makes any difference to your answer). – Jess May 06 '22 at 14:00
  • At this time, I would not worry too much about other people running it and focus on getting it to run on your system. Pycharm has a way of doing this: https://stackoverflow.com/questions/42746732/use-conda-environment-in-pycharm. And, I see below that you are adding it to your path. However, it is generally a good idea to create a new env for, either every new project, or every type of project, or something like this. It encourages using updated packages and also helps to avoid conflicts. And then, each time you work on a particular project, you make sure that the correct env is activated. – Nesha25 May 06 '22 at 16:03

1 Answers1

1

Not a Windows user, but conda activate is for interactive shell sessions. The conda run command is for programmatic execution within an environment. So, you would have a script with a line like:

conda run -n my_env python your_script.py

or possibly

conda run -p /path/to/my_env python your_script.py

if trying to share the environment across users.

If the script requires interaction, you may need to add flags (like --live-stream and/or --no-capture-output). See conda run --help.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Ah I see now that this doesn't work for me as I didn't add conda to my path. So when I do this (have to wait for an admin to let me at work...) then I can save this line of code as a .cmd and fingers crossed that should be all? Thank you! – Jess May 06 '22 at 14:26
  • You can add it to your user level path, probably without an admin. Same place - edit the environment variables, but then you look in the part of the window that is "User variables for ' and then edit that path. – Nesha25 May 06 '22 at 16:00
  • I have now added conda and python to my path (thank you @Nesha25, I didn't need admin!). So I then tried to run my script in the command prompt with: `conda run -n Ngon_env python C:\Users\jlp\Desktop\Local_BLAST_scripts\Neisseria\ngon_script.py --live-stream` But I get the following error which seems to occur after it tried to use the input function in my python script: EOFError: EOF when reading a line – Jess May 09 '22 at 13:06
  • @Jess try putting the `--live-stream` flag before any of the commands (i.e., before "`python`"). Otherwise, it might get interpreted as an argument to the script, rather than the `conda run` command. Also, add this info to your question, under a section of "What I've tried so far" – merv May 09 '22 at 14:43
  • @merv thank you, I have now edited my question and tried your suggestion but now it seems stuck. If anyone knows a more technical term for it 'getting stuck' that would be really helpful as my google searches don't come up with anything useful! – Jess May 09 '22 at 15:35
  • @merv the --no-capture-output flag in the position you recommended solved my problems and all is working now, thanks so much for your help! :) – Jess May 09 '22 at 15:46