When you run a script, you usually do so as follows:
$ python path/to/script.py
However, when running a module, you run it as:
$ python -m path.to.module
It can sometimes be annoying to run a module from the command line because the . separators keep bash from being able to do tab-completion. Is there a custom tab-completion script out there that could handle this situation?
Related
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')
I have a .py python script and when I run it typing ./filename.py I obtain a syntax error. However, when I run it typing python filename.py my program executes correctly.
How to make it run correctly typing ./filename.py ? I think it is related to the $PATH variable but I don't have any further idea.
put either a shebang at the start of the file, or run with python
for example:
#!/usr/bin/env python3
or run:
python3 filename.py
I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.
This is part of my shell script:
#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py
When I run the crontab which executes the shell script, the log file shows the following error:
line 9: python3: command not found
I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.
Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.
My operating system is Linux CentOS.
Hope someone can give me some tips!
You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.
in file.py add first line like below and add +x permission to file.py file
#!/usr/bin/python3
it will automatically execute, no need to mention python3 in the script
use "which python3" command to know exact path of python3 in your machine
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'])
I tried to make a cron job on crontab which runs a python script on AWS ec2. My python script includes a module that is only available for python3.
Using the following command I changed the ec2 default python interpreter from python2.7 to python3.4
Source /home/ec-2user/venv/python34/bin/activate
and then using pip install, I installed the required module for python3.4. So now the default interpreter is python3.4 and when I run the script on ec2-user directory using the following command:
python test.py
the program runs without any problem (so I am sure the module is installed correctly).
But when I assign python file to a cronjob
* * * * * python test.py
It does not work. Checking the mail, the error is:
“No module found named “xxxxx” “
But as I said it worked fine outside of the cron.
I was wondering if you can help me with this problem. I appreciate your time and information.
You have to make a shell script which will do the steps of changing to script directory, activating virtual environment and then running it.
Example:
#!/bin/bash
cd $YOUR_DIR
. venv/bin/activate
python3.4 test.py
Then you call this script in cron with
/bin/bash /.../script.sh
What you could do additionally is
chmod +x test.py
and add/update first line to:
#!/usr/bin/env python3.4
This way you can just run Python script with ./test.py
Create file as 'user_cron.sh'
#!/bin/bash
cd '/root/my_new_project_python'
. my_project_venv/bin/activate
python3 main.py
set the cron using crontab -e