Changing version of python when using nohup - python

I am trying to run a python program called compare.py with the linux nohup command which keeps the program running until it is done without interruption. my python program has packages which can only run on python 2.7 and when i use nohup command program is run in python 2.6. how do i change the version of python when using nohup?
Example: nohup python compare.py $
I tried doing:
alias python=python2.7
before starting program and version of python isn't switched. how do i switch the version of python to 2.7 when i run nohup?

The easiest way would be to use a shebang line to specify the interpretter. At the start of your Python file, put something like
#!/usr/bin/python2.7
# This should be a path to an interpreter that you know for sure is Python 2.7
Then, use chmod +x file.py to make the Python file itself executable, and omit the python part of your nohup command, eg. nohup ./compare.py.

I had the same issue with anaconda python. While using nohup python, it was using python 2.7 but generic python command in terminal was taking me to 3.6.
nohup ~/anaconda3/bin/python scriptname.py
Providing full path to python after nohup command will solve the issue

Related

Add Python terminal in VS Code

I am trying to run Python from VS Code. I have already activated python through the terminal. However, in the terminal selector in the lower right of the screen, I cannot find Python terminal option there:
I wish to ask how could I make Python terminal visible here so that the next time I wish to switch terminal to Python, I do not have to load a .py file but could just selecting Python terminal directly. Thank you.
Python terminals aren't really a thing. If you want to run a .py file, do $ python [filename].py or $ python3 [filename].py. If you want to just run commands, open a Python shell by doing $ python or $ python3
There is no Python terminal. If you are asking about loading Python shell you just have to type python into your terminal to load the Python shell

Python cannot run in console mode

I use Anaconda3 installed python on Windows10.
When I run python --version in git-bash, it works.
$ python --version
Python 3.7.3
$ which python
/d/Programs/Anaconda3/python
But when I run python, it output nothing and pending there
$ python
A image may be more clear here.
I can input anything, when press, but nothing happens, just a new line.
$ python
print(1)
Note My issue is different from Python not working in command prompt? which is cannot find the python path.
Try all your commands with $ python3 ... maybe you dont have installed the python 2 version which gets normaly called by $ python ..
I think here has something to do with git-bash GUI. I cannot run it in git-bash, but I can run it in windows cmd.

Why does Subprocess.Popen run a different Python version to cmd.exe?

I have a script which won't run inside of Nuke's built-in Python interpreter, so I'm trying to launch in via the system's Python instead. I'm using subprocess.Popen to do this, but it still won't run inside a subprocess (missing modules), even though it runs fine at the command prompt (cmd.exe)
I think the problem is, that I just don't understand what environment my subprocess starts up in. It doesn't even run the expected version of Python.
In cmd.exe:
C:/Python27/python.exe -V
> Python 2.7.16
In Nuke:
import subprocess as sub
print sub.Popen("C:/Python27/python.exe -V", stderr=sub.PIPE).communicate()[1]
# Result: Python 2.7.13
print sub.Popen('C:/Python27/python.exe -c
"import sys;print(sys.executable)"',
stdout=sub.PIPE).communicate()[0]
# Result: C:\Python27\python.exe
Where is this lower version of Python coming from? It's probably not a coincidence that Nuke's built-in Python is also 2.7.13, but why would Popen run that and not the .exe I'm specifying?
Note: same result whether I give shell=True or shell=False

How to execute python3 program in shell script

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

starting a python 3.3. script at ubuntu startup

The standard python version of ubuntu 13.04 is python 2.7.
I know that I can call a python script of version 3.3 by calling python3.3 or python3 in terminal instead of only "python", which starts the version 2.7...
e.g. python3 myscript.py
But now I have a version 3.3. script in the system start routine and can only tell the path to the file. The system recognizes it as a python script (in the shebang with #!/usr/bin/python3)
But how to open it with the correct version? It is tried to be opened with the standard python install so it wont work nor even show up.
The shebang line #!/usr/bin/python3 should work if sh, bash, etc. is trying to launch your script.
It it is being run from another script as python myscript.py you'll have to find that script and get it to launch the script using python3 myscripy.py

Categories