I am working on the project where I need to run my script daily at a specific time. I am using crontab/cronjob to run shell script. Using shell script I want to activate virtualenv and python interpreter to run some commands and deactivate it. I have tried
#!/bin/bash
bash
source virtual_env/bin/activate
cd src
python script.py
but didn't work for me
Note: I can activate my virtualenv and use /home/bin/virtual_env/python the interpreter manually but i wanted to it via shell script.
Often there is no need to activate a virtual environment:
#!/usr/bin/env sh
PYTHON_BIN='/path/to/virtual_environment/bin/python'
pushd 'src'
"${PYTHON_BIN}" 'script.py'
Related
I created a virtual environment for a Python program using PyCharm. I can run the program just fine straight from PyCharm terminal but when I try to run it from windows command prompt I get an error because some modules in the program are only installed in the virtual environment.
So my question is: how do I run a Python program that is in a virtual environment from the command prompt?
$ cd your_project
$ ./activate.bat
$ python helloworld.py
Explaination: virtualenv should create a .bat file in your project root directory. To run it, activate the virtual env first then run the python command.
I think you have to look after switching virtual environment on windows
may be this will help
[https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195][1]
I am trying to run a python script under python 2 virtualenv. How to run it via batch script/ python script?
I have installed both python2 and python 3 and created virtual env too. I tried it invoking via python script but it didn't even enter the virtualenv. Then i tried the below batch script. But It just executed the first line of the code. i.e just activating the virtual environment. but other lines are not getting executed.
I even tried to execute the 1st line of batch script separately in a bat file and then invoke others using perl/python. but none of them worked.
Please do provide a way to execute these commands either using python script or
a batch file, which I will need to run it via perl/python
The batch file i used :
C:\venv-2\Scripts\activate
pushd <some path>
python test.py
deactivate
Just directly use the virtualenv's Python interpreter:
pushd some_path
c:\venv-2\scripts\python test.py
popd
Not having a Windows environment to hand, this is a non-answer for *nix which might be adapted to Windows.
Instead of calling the venv bin directly, try activating it within a script. For example:
# my-script.sh
# activate
source venv/bin/activate
# this should be in the venv
which python3
python3 -c 'print("Hello from python3")'
Then $ bash my-script.sh should print out which python3 it thinks is in use within the script.
See also A Python script that activates the virtualenv and then runs another Python script? for the same idea done better...
I got the the batch file executed by giving,
C:\venv-2\Scripts\activate & pushd <some path> & python test.py & deactivate
inside the batch file. And used perl script to call the batch file
system('start test.bat')
When I run source venv/bin/activate in command line , it activates the virtualenv. However when run this via a shell script ./run.sh , I don't see the virtualenv being activated. Similar scripts used to work for me in the past , however I am not sure what I am missing now. I am running this is on a mac.
#! /bin/bash
source venv/bin/activate
(venv) 8c859072374671e:my-project tee78$
When you are running source inside a script. It is running in a new environment. It won't get reflected in the parent shell.
$ cat run.sh
#! /bin/bash
source venv/bin/activate
It you need that to happen do, source your script,
source run.sh
Also, you won't need the shebang line :)
I am trying to create a bash script that will simply
Activate my venv
Launch a python script
that can be double clicked and executed on Mac.
My script is as follows:
#!/usr/bin/env bash
./macVenv/bin/activate
python main.py
I can run from command line just fine. When I double click though it complains it cannot find the python file.
Yes. The following script works on my machine.
source activate $1
python $2
I run it by typing ./script_name py36 python_file_name
where script_name is the name of the script with these 2 lines. py36 is the name of the virtual environment and python_file_name is the python script you want to run.
I needed a module for a shell script I've written in Python, so I used pipenv to install it. I can run the command fine using:
~$ pipenv run python3 foo
Now, if I want to just run ~$ foo on the command line (fish shell on MacOS, homebrew installed), how do I invoke the pipenv environment in the shebang of my Python script? Or is there a better way?
As documented here https://pipenv.readthedocs.io/en/latest/ you need to activate the virtual environment first. This will spawn another shell with the virtual environment activated
$ pipenv shell
so that you can run
$ python foo
to execute your script. Then you can use
#!/usr/bin/env python
on the first line of your script and make the script executable (chmod +x foo.py) so that you can run
$ ./foo
If the location of that script is part of your PATH environment variable, you should be able to run now
$ foo.py
If you don't like the extension you will have to remove from your script too
With pipenv-shebang you can run your script with
pipenv-shebang PATH/SCRIPT
or you could insert the shebang
#!/usr/bin/env pipenv-shebang
to run it with just PATH/SCRIPT.
create wrapper file like below works for me, but little bit hacky way.
import subprocess
if __name__ == '__main__':
subprocess.run(['pipenv', 'run', 'foo'])