How can I use PyMOL API from Python? - python

I have PyMOL already installed on my Linux machine. I know it is installed because when I write pymol -cp pymol_api_test.py the script executes.
I want to run the following python script using python3:
import pymol
from pymol import cmd
print(cmd.align("/home/bbq_spatial/bbq_input_pdb/pdb1a6j.pdb",
"/home/bbq_spatial/bbq_output_pdb/pdb1a6j.pdb",
cycles=0, transform=0))
However, it doesn't run when I call it using python3:
user_name#server_name:~$ nano pymol_api_test.py
user_name#server_name:~$ python3 pymol_api_test.py
Traceback (most recent call last):
File "pymol_api_test.py", line 1, in <module>
import pymol
ModuleNotFoundError: No module named 'pymol'
user_name#server_name:~$
How can I resolve this?

Install conda (or pip) as a package manger for Python.
And then install pymol with the following commands:
conda install -c conda-forge -c schrodinger pymol-bundle
You can check your installed packages in your env with the following command:
conda list
Use this link to install conda on your Linux machine.
Pay attention,PyMol may require other dependencies on you system, check the official guides for installation.
UPDATE:
Check a specific pkg:
with pip: pip show <pkg>
with conda: conda show <pkg>
List all installed pkgs:
with pip: pip list
with conda: conda list

Make sure that your python interpreter has access to the pymol package. This can be done adding it to the PYTHONPATH environment variable.
$ export PYTHONPATH=/opt/pymol/lib/python3.10/site-packages/:$PYTHONPATH
In linux, the pymol command is not a binary but just a shell script to run the corresponding python package. So you can check it to get the correct path to be added.
$ cat $(which pymol)
#!/bin/sh
exec "/usr/bin/python3" "/opt/pymol/lib/python3.10/site-packages/pymol/__init__.py" "$#"

Related

Lib error while executing python file with WSL (ModuleNotFoundError: No module named 'PIL')

First of all, sorry about my english, this isn't my native language.
PS C:\Users\...... wsl ./Test.py
Traceback (most recent call last):
File "./Test.py", line 2, in <module>
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
I made this Test.py using pycharm. Its a wordcloud program and it works fine with pycharm.
However, when I try to execute this file with wsl ./ it shows me the error above. Can someone help?
According to your comment you are checking if Pillow is installed in Windows, but you are running your Windows-hosted Test.py in WSL, i.e. using python interpreter and libraries that are installed in WSL.
To illustrate, Powershell session in Windows, using python libpath from conda:
(base) PS C:\> python -c "import os; print(os.__file__)"
C:\Users\ml\Miniconda3\lib\os.py
Powershell session in Windows, but executing code in WSL, python and libpath from Ubuntu:
(base) PS C:\> wsl python3 -c "import os; print(os.__file__)"
/usr/lib/python3.8/os.py
You do need to check your Pillow installation, but in WSL.
You most likely do not have Pillow installed, and the code wants it to run.
To install it you should just need to run
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
or you can follow the instructions on the pillow website
Hope that helps [:

Python on RasPi can't find installed module

I feel like this has to have been asked and solved already, but I couldn't find a solution that works for me. I pip3'd a python library, and verify it is indeed on my system.
pi#raspberrypi:~/Desktop $ pip3 install pyftpdlib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyftpdlib in /home/pi/.local/lib/python3.7/site-packages (1.5.6)
Then I try to import it but raspi cant find it...
pi#raspberrypi:~/Desktop $ sudo python3 FTPserver2.py
Traceback (most recent call last):
File "FTPserver2.py", line 1, in <module>
import pyftpdlib
ModuleNotFoundError: No module named 'pyftpdlib'
Huh?
When you run pip3 install without sudo, the package gets installed under /home/pi/.local/lib/python3.7/site-packages which is a user-specific location and packages installed there will not be accessible system-wide.
Then you run sudo python3 which makes you execute python3 as the root user which is a different user.
Below I assume you do need to run the command with sudo. If you're not sure, try dropping the sudo - then the import should work (but maybe other stuff will not - I don't know what's in your script).
One method of installing a package for use by root would be to do sudo pip3 install pyftpdlib but this not recommended as it could break the Python installation used by the OS (some packages could have to be updated in order to be compatible with pyftpdlib, but they could then become incompatible with other stuff, and then you're in a lot of trouble).
It is better to use a virtual environment. For example:
# create the virtual environment
$ python3 -m venv env-ftp
# install the package into it
$ env-ftp/bin/python -m pip install pyftpdlib
# run a script using the Python installation contained within the virtual environment
$ sudo env-ftp/bin/python -m Desktop/FTPserver2.py
You could also choose to source env-ftp/bin/activate in order to temporarily switch to using python and pip specific to this environment until you deactivate.
Virtual environments are useful for creating isolated Python installations with their own separate sets of packages, which allows you e.g. to simultaneously use applications that have incompatible sets of dependencies (suppose 1 application requires requests==2.22.0 and another one requires requests<=2.21.0 and won't work with requests==2.22.0).
Can you try running this in the command line without errors?:
python3 -c "import pyftpdlib"
If this returns no error, that means your python interpreter is different. Make sure you are not running different python versions and/or have created different images and have not used sudo privileges to install packages.
As you can see, the pip3 has installed it in the site packages of /home/pi/.local/lib/python3.7/
Run this in command line
python3 -c "import site; print(site.getsitepackages())"
and check if it returns the same path as pip.
PS: It is always recommended to run pip3 install --user instead of sudo pip3 install.

Why does numpy import behave differently?

I used the following commands to install numpy in my python3 virtual environment on Ubuntu 16.04 LTS machine.
My goal is to use python 3.5 by default in my venv and learn numpy. I shouldn't have to explicitly use python3. I feel there's some overlap/error which could be a bigger issue if ignored now.
Also, I don't have python 2.x installed in my virtual environment but I have it at system level.
The commands python3 -V and python -V show the same version and both are located at same path. Why does the last command work but the second to last doesn't work?
~/proj1$ virtualenv --no-site-packages -p python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/$USER/proj1/venv/bin/python3
Also creating executable in /home/$USER/proj1/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
~/proj1$ source venv/bin/activate
(venv) ~/proj1$ which pip
/home/$USER/proj1/venv/bin/pip
(venv) ~/proj1$ pip -V
pip 20.0.2 from /home/$USER/proj1/venv/lib/python3.5/site-packages/pip (python 3.5)
(venv) ~/proj1$ pip install numpy
Collecting numpy
Using cached numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl (19.9 MB)
Installing collected packages: numpy
Successfully installed numpy-1.18.1
(venv) ~/proj1$ python -V
Python 3.5.2
(venv) ~/proj1$ python3 -V
Python 3.5.2
(venv) ~/proj1$ which python
/home/$USER/proj1/venv/bin/python
(venv) ~/proj1$ which python3
/home/$USER/proj1/venv/bin/python3
(venv) ~/proj1$ python -c "import numpy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'numpy'
(venv) ~/proj1$ python3 -c "import numpy"
(venv) ~/proj1$
The problem has nothing to do with numpy per se. Rather, what's happening is that the alias python='/usr/bin/python3' prevents your shell from finding the python executable that's first in your execution path (i.e., the executable with the path given by "which python"), which messes with your virtual environment setup. Because of that alias,
python -c "import numpy"
is interpreted as
/usr/bin/python3 -c "import numpy"
Since you installed numpy in a virtual environment, the system-wide Python 3 installation in /usr/bin has by design no knowledge of that numpy installation, so you get that ImportError.
If, on the other hand, you were to run
unalias python
python -c "import numpy"
then python would be taken to be /home/$USER/proj1/venv/bin/python, provided that you had already sourced /home/$USER/proj1/venv/bin/activate, of course, and things would work as you expect.
The moral here is “don’t use which”. bash (which almost everyone uses now) has a builtin command type that shows how a command is interpreted; in particular, type -a python here would show you that it would be your virtual environment’s python, but is in fact aliased to run the one from /usr/bin that is the same version but doesn’t have the same packages installed (because of course the virtual environment’s directories aren’t on its sys.path).

Python 3 cannot find a module

I am unable to install a module called 'jieba' in Python 3 which is running in Jupyter Notebook 6.0.0. I keep getting ModuleNotFoundError: No module named 'jieba' after trying these methods:
1. import jieba
2. pip3 install jieba
Can anyone help? Thank you.
pip3 in the terminal is almost certainly installing your package into a different Python installation.
Rather than have you hunt around for the right installation, you can use Python and Jupyter themselves to ensure you are using the correct Python binary. This relies on three tricks:
You can execute the pip command-line tool as a module by running python -m pip .... This uses the pip module installed for the python command, so you don't have to verify what python installation the pip3 command is tied to.
You can get the path of the current Python interpreter with the sys.executable attribute.
You can execute shell commands from Jupyter notebooks by prefixing the shell command with !, and you can insert values generated with Python code with {expression}
You can combine these to run pip (to install packages or run other commands) against the current Python installation, from Jupyter itself, by adding this into a cell:
import sys
!{sys.executable} -m pip <pip command line options>
To install your jieba package, that makes:
import sys
!{sys.executable} -m pip install jieba
If you are using Anaconda, then you could also install the conda package for jieba; the package does not require any platform-specific dependencies or compilation, but it may be more convenient for you or necessary to install other packages that do have such requirements and have pre-compiled conda packages.
In that case, tell the conda command about your Python executable:
import sys
!conda install --yes --prefix {sys.prefix} <package name>

Ubuntu Python "No module named paramiko"

So I'm trying to use Paramiko on Ubuntu with Python 2.7, but import paramiko causes this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named paramiko
The other questions on this site don't help me since I'm new to Ubuntu.
Here are some important commands that I ran to check stuff:
sudo pip install paramiko
pip install paramiko
sudo apt-get install python-paramiko
Paramiko did "install". These are the only commands I used to "install" paramiko. I'm new to Ubuntu, so if I need to run more commands, lay them on me.
which python
/usr/local/bin/python
python -c "from pprint import pprint; import sys; pprint(sys.path);"
['',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']
In the python interpreter, I ran help("modules") and Paramiko is not in the list.
two paramiko folders are located in usr/local/lib/python2.7/dist-packages.
Short version: You're mixing Ubuntu's packaged version of Python (/usr/bin/python) and a locally built and installed version (/usr/local/bin/python).
Long version:
You used apt-get install python-paramiko to install Ubuntu's official Paramiko package to /usr/lib/python2.7/dist-packages.
You used (I assume) Ubuntu's version of pip, which installs to /usr/local/lib/python2.7/dist-packages. (See here.)
You used a locally built version of Python, and because it's locally built, it uses /usr/local/lib/python2.7 instead of /usr/lib/python2.7, and because it doesn't have Debian/Ubuntu customizations, it doesn't check use dist-packages.
Solution: You should be able to add /usr/local/lib/python2.7/dist-packages to your /usr/local/bin/python's sys.path, but since you're using Ubuntu, it's easiest to let Ubuntu do the work for you:
Use /usr/bin/python instead of a local version.
Use Ubuntu's packages wherever possible (i.e., use apt-get instead of pip).
Use virtualenv for the rest (to keep a clean separation between Ubuntu-packaged and personally installed modules).
I'd go so far as to uninstall the local version of Python and delete /usr/local/lib/python2.7, to ensure that no further mismatches occur. If you don't want to be that drastic, then you can edit your $PATH to put /usr/bin before /usr/local/bin to run the system version of Python by default.
Try downloading the zip file from https://github.com/paramiko/paramiko and running this command in the unzipped directory :
python setup.py install
There are two others methodes for add modules in python :
The first :
Download the package.
Create directory and paste the package in it.
Tap in the terminal :
export PYTHONPATH=$PYTHONPATH:path_of_package
The second :
open python interpreter:
import sys
sys.path.insert(0, "path_of_package")
Try installing only through commands.
Download paramiko package from git using this command: git clone https://github.com/paramiko/paramiko.git
Go to unzipped directory and run export PYTHONPATH=$PYTHONPATH:<path_to_paramiko>
If you find libffi package not found then run this command: sudo apt-get install libffi6 libffi-dev and If you haven't properly installed the header files and static libraries for python dev then run this command: sudo apt-get install python-dev
Enjoy :)
Also, mind the version of python, if the error was reported by python3, then install python3's paramiko.
If you're using Python 3, type the below command
$ sudo -H pip3 install paramiko --ignore-installed
try type pi then tap, this give you this
:$ pi
pic piconv pidstat pinentry-curses ping6
pip3 pivot_root
pic2graph pidof pinentry ping pinky
pip3.6
then you type in whereis pip3
$ whereis pip3
pip3: /usr/local/bin/pip3.6 /usr/local/bin/pip3
xg#xx-ppmaster:/xg/scripts/pyth
$ sudo /usr/local/bin/pip3 install paramiko
This should let you install paramiko
more on python installation
https://danieleriksson.net/2017/02/08/how-to-install-latest-python-on-centos/

Categories

Resources