Predictive Hacks

How to Run a Python Script from a Conda Environment

In a previous post, we explained how to run a python script within a shell script from a conda environment. Now, we will explain how to run a python script by specifying the conda environment. First of all, you need to find the path of your conda environment which can be retrieved by running the command:

conda env list

Assume that our conda environment is the ‘gpipis‘ and the path is the “/home/ds/.conda/envs/gpipis“. What you need to do is to add the shebang line to your script by adding the bin/python to your conda environment. For example, let’s assume that our test.py is the following:

#!/home/ds/.conda/envs/billy/bin/python

f = open("demo_file.txt", "a")
f.write("I added a new line!\n")
f.close()

Maybe you will need to make the file executable by changing the permissions. for example

chmod 744 test.py

And then we can run the script as:

./test.py

This script will run under the gpipis environment. Once we run the file, a new line will be added to the “demo_file.txt”. For example:

cat demo_file.txt
I added a new line!

If we run the script again, a new line will be added. For example:

./test.py

cat demo_file.txt
I added a new line!
I added a new line!

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.