Getting python2.7 path in django app for subprocess call - linux

I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.

To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)

Related

Python script to launch pymol on windows

I have installed the opensource version of pymol on windows (https://github.com/schrodinger/pymol-open-source).
To run pymol, I can open a python3.8 command prompt on windows and type the following:
import pymol
pymol.finish_launching()
This launches the pymol gui.
I'd now like to create a simple python script so that I can click the script (or convert to a windows executible using for example pyinstaller), however if I put the above commands in a script pymol.py and then run with:
python pymol.py
where python points to python3.8 I get the following error:
AttributeError: partially initialize module 'pymol' has no attribute 'finish_launching' most likely due to a circular import.
Why does this work on the command line python but not in a script and how can I fix this so the script runs and can be converted to a windows executible?

ModuleNotFoundError: No module named 'pandas' in a flask program

files used
data.csv - dummy data set i am using
main.py - my core flask program
problem i am facing
even though pandas is installed in my virtual environment(python3.8 - virtualenv) i am getting ModuleNotFoundError for pandas when used in main.py
additional information
There is no error in interactive environment when importing pandas
within the virtual environment.
Same error is thrown by webUI by
flask application.
Your main.py script has the shebang
#!/usr/bin/python
pointing to the system interpreter. If you execute ./main.py, your script is launched with the system interpreter, not the virtual env.
Change the shebang or execute the script with your venv python:
$ python main.py

Command works at the file path, but won't work from root

I'm still a newbie at bash programming, but trying to run a program with little script. Reducing the problem to the error message, I have
cd /full/path/to/program
python3 -m krop
that is the command working when the actual folder is the /full/path/to/program
but if I run the same from root it doesn't work.
cd /another/path
python3 -m /full/path/to/program/krop
/usr/bin/python3: Error while finding module specification for '/full/path/to/program/krop'
(ModuleNotFoundError: No module named 'krop-0')
I tried lot of variants, but always the same output with errors. I do not have a clue of why the library "python3" adds the "-0" at the end of the name of the file.
What should I put to run the program from root?
python -m command expects a module name, similarly to the syntax you would import in a python program. So if your modules lies in ./directory and directory is a valid python module, you can do python -m directory.krop
You can't however index python modules from file system root. You have either to make your bash script run it in the good directory so you make a local import; or you have to package and install your module system-wide to make a global import that would be invoked with python -m krop from anywhere.
More information on packaging and installing modules: https://packaging.python.org/tutorials/packaging-projects/
Problem solved!,
It was a matter of managing the python import paths, as #hiroprotagonist replied. The list that contains all of directories python will use to search for modules, is available in a variable named sys.path.
So, if somebody wants to run a program (a 'library module as a script', according to python help) through python's command, from a directory different from the 'pwd' one, should write in the command line:
export PYTHONPATH='/full/path/to/program/'
python3 -c "import sys; print(sys.path)"
python3 -m krop
The second line is actually to print on screen, but the first one is the only necessary (export PYTHONPATH).
Thank you for the keywords and help!
Ps. May be should be edited the question title to "problem with a python command to run a program from command line on linux" or something like that.
Reference: python --help :)

ModuleNotFoundError: No module named 'win32com'

Actually, I have the same problem as him:
No module named win32com
I have installed pywin32 but MobaXterm tells me "No module named win32com".
However, I am able to run my program using IDLE with no error.
What's the problem?
Code:
import win32com.client
import sys, os
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut('C:/Users/Seaky/Desktop/CS 160.lnk')
os.chdir(shortcut.Targetpath)
What I am doing is that I am trying to do "cdlnk path" in the terminal using python code, where the path is a shortcut folder like the path above instead of a real path.
I used different code for running in the terminal and the IDLE but I only changed the path string from sys.argv[1] to the current one, which should not affect the result.
I have found out. The compiler of python installed in the MobaXterm doesn't have the pywin32 module.

python3 execute script without dependency

My python3 script currently requires pypdf2 for execution.
If I want to run it on another device I would have to install python3, pip and pypdf2.
Is there a way to include pypdf2 into my script so that I only need python itself to run my script?
EDIT:
The script runs on windows (10)
If you want to distribute your script you can do this using PyInstaller as a stand-alone application.
This is a python module that bundles the interpreter with all the required libraries.

Resources