1

To run a Python program I take run the following manually:

conda activate sentence-splitter-service
python3 scripts/serve.py  /opt/acorn/file-stream/acorn-sentence-request-local /opt/acorn/file-stream/acorn-language-request-local /opt/acorn/file-stream/acorn-document-response-local  --port 8887

Can I run both of these in a shell script?

2
  • You should be able to run pretty much anything in a shell script. Have you tried it? What problems did you encounter? Commented Dec 28, 2021 at 20:34
  • Yes, you can. Whether it does what you want it to do is a separate question entirely, but since you haven't told us what the desired behavior is nor given us the contents of your scripts, we can't answer that question for you. Commented Dec 28, 2021 at 20:35

2 Answers 2

5

conda activate is for an interactive shell only (i.e. not a shell script). If you want a specific env to run a script, then use conda run -n sentence-splitter-service python scripts/serve.py ... where -n specifies the environment name. As an example:

# This shows that my virtualenv is indeed on my path
conda run -n testenv python -c 'import sys; print(sys.path)'
['', 'C:\\Users\\cn18933\\Miniconda3\\envs\\testenv\\python38.zip', ...]
Sign up to request clarification or add additional context in comments.

Comments

2

I believe using conda run should be the preferred solution, however, one can also use the shebang to indicate the shell should run in login-mode:

script.sh

#!/usr/bin/env bash -l
conda activate sentence-splitter-service
python3 scripts/serve.py  /opt/acorn/file-stream/acorn-sentence-request-local /opt/acorn/file-stream/acorn-language-request-local /opt/acorn/file-stream/acorn-document-response-local  --port 8887

This assumes that the user executing the script has run conda init to configure their shell resource file (e.g., .bashrc or .bash_profile) to initialize the Conda shell functionality.

2 Comments

I tried the shebang as you have in line 1 above, but got "/usr/bin/env: bash -l: No such file or directory
@Woodsman Can depend on your system. Another variant is simply #!/bin/bash -l (i.e., don't assume bash is currently on PATH). For Windows, I'm not sure what the alternative is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.