Python script to launch pymol on windows - python-3.x

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?

Related

Run python script with module/ modules already imported

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

ModuleNotFoundError: No module named 'docx' on VScode but not in Python itself

I have installed the python-docx module using pip:
python -m pip install python-docx
However, when I try to run my script that only contains import docx, I get the following error
ModuleNotFoundError: No module named 'docx'
When I execute the following command in Python on the command line, it works fine:
import docx
When using Visual Studio Code, make sure that it uses the same Python version as you are using when running the script. You you should set the "python.pythonPath" setting correctly in settings.json (manually edit the file or edit in Visual Studio itself). This can be done system-wide (per user), or per project.
Furthermore, this answer gives some background about how Python files are executed in Windows.

Adding shebang causes No such file or directory error

I have a python file that I need to run. I have made sure to make it executable. I have added a shebang to environment python3 and I have been having no such file or directory error when trying to run this code.
I have tried to convert the line endings to unix format using dos2unix and the error still occurs. The works fine when i run it directly without shebang using python3 file_name.py command.
#!/usr/bin/env python3
import tensorflow as tf
import numpy as np
import roslib
roslib.load_manifest('joint_states_listener')
roslib.load_manifest('spider_control')
import pylab as plt
This python file a custom plugin that has to run with ros. I have made sure to source the catkin environment. Do you think that shebang expression error is caused because I have sourced the terminal to another catkin directory where my ros packages are located ?
I have launched my robot in a terminal using
roslaunch spider_gazebo spider_world.launch
this command says ros to launch the spider_world launch file from spider_gazebo package. This has started all my controllers. Later i opened terminal and I exported my ros environment paths and sourced that terminal to catkin workspace and then used this command
rosrun spider_control control.py
where control.py is my python node and spider_control is the package. the error is
/usr/bin/env: python3.6.7: No such file or directory

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.

Getting python2.7 path in django app for subprocess call

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)

Resources