question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toolkit fails to read Path objects

See original GitHub issue

Describe the bug

Toolkit fails to read file if a Path object is used instead of a str.

To Reproduce

import traceback
from pathlib import Path
​
from openff.toolkit.topology import Molecule
​
SDF_PATH = "ethanol.sdf"
​
# no error
Molecule.from_file(SDF_PATH)
​
# Exception: If providing a file-like object for reading molecules, the format must be specified
try:
    Molecule.from_file(Path(SDF_PATH))
except Exception as e:
    traceback.print_exception(e)
​
# But this doesn't seem to work either
try:
    Molecule.from_file(Path(SDF_PATH), file_format="sdf")
except Exception as e:
    traceback.print_exception(e)

Output

Traceback (most recent call last):
  File "/home/mmh/Projects/scratch/openff-toolkit-test.py", line 13, in <module>
    Molecule.from_file(Path(SDF_PATH))
  File "/home/mmh/miniconda3/envs/openff-toolkit-test/lib/python3.10/site-packages/openff/toolkit/topology/molecule.py", line 4625, in from_file
    raise Exception(
Exception: If providing a file-like object for reading molecules, the format must be specified
Traceback (most recent call last):
  File "/home/mmh/Projects/scratch/openff-toolkit-test.py", line 19, in <module>
    Molecule.from_file(Path(SDF_PATH), file_format="sdf")
  File "/home/mmh/miniconda3/envs/openff-toolkit-test/lib/python3.10/site-packages/openff/toolkit/topology/molecule.py", line 4718, in from_file
    raise Exception("Unable to read molecule from file: {}".format(file_path))
Exception: Unable to read molecule from file: ethanol.sdf

Computing environment (please complete the following information):

  • Operating system
$ uname -a
Linux d174 5.17.15-76051715-generic #202206141358~1655919116~22.04~1db9e34 SMP PREEMPT Wed Jun 22 19 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/os-release
NAME="Pop!_OS"
VERSION="22.04 LTS"
ID=pop
ID_LIKE="ubuntu debian"
PRETTY_NAME="Pop!_OS 22.04 LTS"
VERSION_ID="22.04"
HOME_URL="https://pop.system76.com"
SUPPORT_URL="https://support.system76.com"
BUG_REPORT_URL="https://github.com/pop-os/pop/issues"
PRIVACY_POLICY_URL="https://system76.com/privacy"
VERSION_CODENAME=jammy
UBUNTU_CODENAME=jammy
LOGO=distributor-logo-pop-os
  • Output of running conda list
# packages in environment at /home/mmh/miniconda3/envs/openff-toolkit-test:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
amberlite                 22.0                     pypi_0    pypi
ambertools                22.0            py310hfade12a_1    conda-forge
amberutils                21.0                     pypi_0    pypi
arpack                    3.7.0                hdefa2d7_2    conda-forge
astunparse                1.6.3              pyhd8ed1ab_0    conda-forge
black                     22.6.0             pyhd8ed1ab_0    conda-forge
blosc                     1.21.1               h83bc5f7_3    conda-forge
boost                     1.74.0          py310h7c3ba0c_5    conda-forge
boost-cpp                 1.74.0               h75c5d50_8    conda-forge
brotli                    1.0.9                h166bdaf_7    conda-forge
brotli-bin                1.0.9                h166bdaf_7    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
c-ares                    1.18.1               h7f98852_0    conda-forge
ca-certificates           2022.6.15            ha878542_0    conda-forge
cairo                     1.16.0            ha61ee94_1011    conda-forge
certifi                   2022.6.15       py310hff52083_0    conda-forge
click                     8.1.3           py310hff52083_0    conda-forge
cloudpathlib              0.9.0           py310hff52083_0    conda-forge
cudatoolkit               11.7.0              hd8887f6_10    conda-forge
curl                      7.83.1               h7bff187_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
cython                    0.29.30         py310hd8f1fbe_0    conda-forge
dataclasses               0.8                pyhc8e2a94_3    conda-forge
expat                     2.4.8                h27087fc_0    conda-forge
fftw                      3.3.10          nompi_h77c792f_102    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.0               h8e229c2_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.34.4          py310h5764c6d_0    conda-forge
freetype                  2.10.4               h0708190_1    conda-forge
gettext                   0.19.8.1          h73d1719_1008    conda-forge
giflib                    5.2.1                h36c2ea0_2    conda-forge
greenlet                  1.1.2           py310hd8f1fbe_2    conda-forge
hdf4                      4.2.15               h10796ff_3    conda-forge
hdf5                      1.12.1          nompi_h2386368_104    conda-forge
icu                       70.1                 h27087fc_0    conda-forge
importlib-metadata        4.11.4          py310hff52083_0    conda-forge
isort                     5.10.1             pyhd8ed1ab_0    conda-forge
jpeg                      9e                   h166bdaf_2    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
kiwisolver                1.4.3           py310hbf28c38_0    conda-forge
krb5                      1.19.3               h3790be6_0    conda-forge
lcms2                     2.12                 hddcbb42_0    conda-forge
ld_impl_linux-64          2.36.1               hea4e1c9_2    conda-forge
lerc                      3.0                  h9c3ff4c_0    conda-forge
libblas                   3.9.0           15_linux64_openblas    conda-forge
libbrotlicommon           1.0.9                h166bdaf_7    conda-forge
libbrotlidec              1.0.9                h166bdaf_7    conda-forge
libbrotlienc              1.0.9                h166bdaf_7    conda-forge
libcblas                  3.9.0           15_linux64_openblas    conda-forge
libcurl                   7.83.1               h7bff187_0    conda-forge
libdeflate                1.12                 h166bdaf_0    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libev                     4.33                 h516909a_1    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 12.1.0              h8d9b700_16    conda-forge
libgfortran-ng            12.1.0              h69a702a_16    conda-forge
libgfortran5              12.1.0              hdcd56e2_16    conda-forge
libglib                   2.72.1               h2d90d5f_0    conda-forge
libgomp                   12.1.0              h8d9b700_16    conda-forge
libiconv                  1.16                 h516909a_0    conda-forge
liblapack                 3.9.0           15_linux64_openblas    conda-forge
libnetcdf                 4.8.1           nompi_h329d8a1_102    conda-forge
libnghttp2                1.47.0               h727a467_0    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libopenblas               0.3.20          pthreads_h78a6416_0    conda-forge
libpng                    1.6.37               h753d276_3    conda-forge
libssh2                   1.10.0               ha56f1ee_2    conda-forge
libstdcxx-ng              12.1.0              ha89aaad_16    conda-forge
libtiff                   4.4.0                hc85c160_1    conda-forge
libuuid                   2.32.1            h7f98852_1000    conda-forge
libwebp                   1.2.2                h3452ae3_0    conda-forge
libwebp-base              1.2.2                h7f98852_1    conda-forge
libxcb                    1.13              h7f98852_1004    conda-forge
libzip                    1.9.2                hc869a4a_0    conda-forge
libzlib                   1.2.12               h166bdaf_1    conda-forge
lz4-c                     1.9.3                h9c3ff4c_1    conda-forge
lzo                       2.10              h516909a_1000    conda-forge
matplotlib-base           3.5.2           py310h5701ce4_0    conda-forge
mdtraj                    1.9.7           py310hd8d60c7_1    conda-forge
mmpbsa-py                 16.0                     pypi_0    pypi
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
mypy_extensions           0.4.3           py310hff52083_5    conda-forge
ncurses                   6.3                  h27087fc_1    conda-forge
netcdf-fortran            4.5.4           nompi_h2b6e579_100    conda-forge
networkx                  2.8.4              pyhd8ed1ab_0    conda-forge
nomkl                     1.0                  h5ca1d4c_0    conda-forge
numexpr                   2.8.0           py310hf05e7a9_102    conda-forge
numpy                     1.23.1          py310h53a5b5f_0    conda-forge
ocl-icd                   2.3.1                h7f98852_0    conda-forge
ocl-icd-system            1.0.0                         1    conda-forge
openff-forcefields        2.0.0              pyh6c4a22f_0    conda-forge
openff-toolkit            0.10.6             pyhd8ed1ab_0    conda-forge
openff-toolkit-base       0.10.6             pyhd8ed1ab_0    conda-forge
openjpeg                  2.4.0                hb52868f_1    conda-forge
openmm                    7.7.0           py310hccf1d78_1    conda-forge
openssl                   1.1.1q               h166bdaf_0    conda-forge
packaging                 21.3               pyhd8ed1ab_0    conda-forge
packmol                   20.010               h86c2bf4_0    conda-forge
packmol-memgen            1.2.3rc0                 pypi_0    pypi
pandas                    1.4.3           py310h769672d_0    conda-forge
parmed                    3.4.3           py310hd8f1fbe_2    conda-forge
pathspec                  0.9.0              pyhd8ed1ab_0    conda-forge
pcre                      8.45                 h9c3ff4c_0    conda-forge
pdb4amber                 22.0                     pypi_0    pypi
perl                      5.32.1          2_h7f98852_perl5    conda-forge
pillow                    9.2.0           py310he619898_0    conda-forge
pip                       22.1.2             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               h36c2ea0_0    conda-forge
platformdirs              2.5.1              pyhd8ed1ab_0    conda-forge
pthread-stubs             0.4               h36c2ea0_1001    conda-forge
pycairo                   1.21.0          py310h96fc21a_1    conda-forge
pyparsing                 3.0.9              pyhd8ed1ab_0    conda-forge
pytables                  3.7.0           py310hf5df6ce_0    conda-forge
python                    3.10.5          h582c2e5_0_cpython    conda-forge
python-constraint         1.4.0                      py_0    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python_abi                3.10                    2_cp310    conda-forge
pytraj                    2.0.6                    pypi_0    pypi
pytz                      2022.1             pyhd8ed1ab_0    conda-forge
pyupgrade                 2.37.1             pyhd8ed1ab_0    conda-forge
rdkit                     2022.03.4       py310h1c297d8_0    conda-forge
readline                  8.1.2                h0f457ee_0    conda-forge
reportlab                 3.5.68          py310h94fcab3_1    conda-forge
sander                    22.0                     pypi_0    pypi
scipy                     1.8.1           py310h7612f91_0    conda-forge
setuptools                63.1.0          py310hff52083_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
smirnoff99frosst          1.1.0              pyh44b312d_0    conda-forge
snappy                    1.1.9                hbd366e4_1    conda-forge
sqlalchemy                1.4.39          py310h5764c6d_0    conda-forge
sqlite                    3.39.0               h4ff8645_0    conda-forge
tk                        8.6.12               h27826a3_0    conda-forge
tokenize-rt               4.2.1              pyhd8ed1ab_0    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
typed-ast                 1.5.4           py310h5764c6d_0    conda-forge
typing-extensions         4.3.0                hd8ed1ab_0    conda-forge
typing_extensions         4.3.0              pyha770c72_0    conda-forge
tzdata                    2022a                h191b570_0    conda-forge
unicodedata2              14.0.0          py310h5764c6d_1    conda-forge
wheel                     0.37.1             pyhd8ed1ab_0    conda-forge
xmltodict                 0.13.0             pyhd8ed1ab_0    conda-forge
xorg-kbproto              1.0.7             h7f98852_1002    conda-forge
xorg-libice               1.0.10               h7f98852_0    conda-forge
xorg-libsm                1.2.3             hd9c2040_1000    conda-forge
xorg-libx11               1.7.2                h7f98852_0    conda-forge
xorg-libxau               1.0.9                h7f98852_0    conda-forge
xorg-libxdmcp             1.1.3                h7f98852_0    conda-forge
xorg-libxext              1.3.4                h7f98852_1    conda-forge
xorg-libxrender           0.9.10            h7f98852_1003    conda-forge
xorg-libxt                1.2.1                h7f98852_2    conda-forge
xorg-renderproto          0.11.1            h7f98852_1002    conda-forge
xorg-xextproto            7.3.0             h7f98852_1002    conda-forge
xorg-xproto               7.0.31            h7f98852_1007    conda-forge
xz                        5.2.5                h516909a_1    conda-forge
zipp                      3.8.0              pyhd8ed1ab_0    conda-forge
zlib                      1.2.12               h166bdaf_1    conda-forge
zstd                      1.5.2                h8a70e8d_2    conda-forge

Additional context

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mattwthompsoncommented, Jul 11, 2022

My quick impression is that we should use Path objects internally where possible, which I understand to be a popular suggestion[^1]. But we should convert out to str or another object that we trust the wrapped toolkits will digest. This should enable Molecule.from_file(path_object) to work as a user would expect, although maybe a bit trickier to ensure ToolkitWrapper.from_file and similar methods have the same input sanitization.

[^1]: I’m apparently forgetful enough to raise the same feature request twice! #943 #740

1reaction
mattwthompsoncommented, Jul 11, 2022

Great - looks like we have a path forward here. I’ll first to the band-aid fix of making our public API not choke on Path objects and, if that’s as painless as I expect it to be, consider doing more internal refactors. I do want to get this out pretty quickly, though, so I might not refactor everything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support pathlib.Path in place of path strings · Issue #740 ... - GitHub
Some, likely many, I/O operations work on path strings but not pathlib.Path objects. >>> from openforcefield.topology import Molecule >>> from pathlib ...
Read more >
File paths and extensions excluded in Toolkit backup - Seagate
The Toolkit backup will not back up Hidden files, or System files. The following provides a detailed list of file paths and extensions...
Read more >
pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Read more >
Python 3's pathlib Module: Taming the File System
In this tutorial, you have seen how to create Path objects, read and write files, manipulate paths and the underlying file system, as...
Read more >
Using eslint with typescript - Unable to resolve path to module
In my eslintrc.js config file, the "import/resolver" object needed to sit within the "rules" node, not the " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found