Skip to content

__file__ always points to __init__.py #484

Description

@TristynAlxander

Describe the bug

__file__ variable in python scipts is pointing to __init__.py instead of the respective script file.

To Reproduce

Steps to reproduce the behavior:

  1. Make file 'temp_0.py'
print("__file__")
print(__file__)
  1. run file through pymol __init__.py. I uses a short described below to simply run pymol temp_0.py Equivalently, you can run python /path/to/pymol/__init__.py temp_0.py

Expected behavior

Currently __file__ points to __init__.py, not temp_0.py. __file__ in temp_0.py should point totemp_0.py

I'm not exactly sure where the exec() function is in the pymol code, but essentially when you convert the temp_0.py file to a string and pass it to exec you need to modify the namespace you pass to the exec function such that __file__ points to the path of whatever you're converting to a string.

Something like this, but be careful of whether you feed it to globals or locals and it'll probably modifying the __file__ in some pre-existing namespace. Not sure about the details...

path = "/home/user/documents/folder/temp_0.py"
path = fix_path(path)
exec_str = file_to_str(path)
namespace = {"__file__": path,}
exec(exec_str, namespace)

Environment

  • PyMOL Version: 3.1.0a0
  • PyMOL Source: compiled from this repo
  • Operating System: Linux Mint
  • If you are using a conda environment, please supply your conda list:
# packages in environment at /opt/.miniconda/envs/protein_pymol:
#
# Name                       Version          Build                Channel
_libgcc_mutex                0.1              conda_forge          conda-forge
_openmp_mutex                4.5              2_gnu                conda-forge
brotli                       1.1.0            hb9d3cd8_2           conda-forge
brotli-bin                   1.1.0            hb9d3cd8_2           conda-forge
brotli-python                1.1.0            py313h46c70d0_2      conda-forge
bzip2                        1.0.8            h4bc722e_7           conda-forge
ca-certificates              2024.12.14       hbcca054_0           conda-forge
cairo                        1.18.2           h3394656_1           conda-forge
cctbx-base                   2024.12          pypi_0               pypi
certifi                      2024.12.14       pyhd8ed1ab_0         conda-forge
cffi                         1.17.1           py313hfab6e84_0      conda-forge
chardet                      5.2.0            py313h78bf25f_2      conda-forge
charset-normalizer           3.4.1            pyhd8ed1ab_0         conda-forge
cmake                        3.31.4           pypi_0               pypi
contourpy                    1.3.1            py313h33d0bda_0      conda-forge
cycler                       0.12.1           pyhd8ed1ab_1         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             h77eed37_3           conda-forge
fontconfig                   2.15.0           h7e30c49_1           conda-forge
fonts-conda-ecosystem        1                0                    conda-forge
fonts-conda-forge            1                0                    conda-forge
fonttools                    4.55.3           py313h8060acc_1      conda-forge
freetype                     2.12.1           h267a509_2           conda-forge
freetype-py                  2.3.0            pyhd8ed1ab_0         conda-forge
future                       1.0.0            pyhd8ed1ab_1         conda-forge
h2                           4.1.0            pyhd8ed1ab_1         conda-forge
hpack                        4.0.0            pyhd8ed1ab_1         conda-forge
hyperframe                   6.0.1            pyhd8ed1ab_1         conda-forge
icu                          75.1             he02047a_0           conda-forge
idna                         3.10             pyhd8ed1ab_1         conda-forge
kiwisolver                   1.4.7            py313h33d0bda_0      conda-forge
lcms2                        2.16             hb7c19ff_0           conda-forge
ld_impl_linux-64             2.43             h712a8e2_2           conda-forge
lerc                         4.0.0            h27087fc_0           conda-forge
libblas                      3.9.0            26_linux64_openblas  conda-forge
libboost                     1.86.0           h6c02f8c_3           conda-forge
libboost-python              1.86.0           py313hf0ab243_3      conda-forge
libbrotlicommon              1.1.0            hb9d3cd8_2           conda-forge
libbrotlidec                 1.1.0            hb9d3cd8_2           conda-forge
libbrotlienc                 1.1.0            hb9d3cd8_2           conda-forge
libcblas                     3.9.0            26_linux64_openblas  conda-forge
libdeflate                   1.23             h4ddbbb0_0           conda-forge
libdrm                       2.4.124          hb9d3cd8_0           conda-forge
libegl                       1.7.0            ha4b6fd6_2           conda-forge
libexpat                     2.6.4            h5888daf_0           conda-forge
libffi                       3.4.2            h7f98852_5           conda-forge
libgcc                       14.2.0           h77fa898_1           conda-forge
libgcc-ng                    14.2.0           h69a702a_1           conda-forge
libgfortran                  14.2.0           h69a702a_1           conda-forge
libgfortran5                 14.2.0           hd5240d6_1           conda-forge
libgl                        1.7.0            ha4b6fd6_2           conda-forge
libglib                      2.82.2           h2ff4ddf_1           conda-forge
libglu                       9.0.3            h03adeef_0           conda-forge
libglvnd                     1.7.0            ha4b6fd6_2           conda-forge
libglx                       1.7.0            ha4b6fd6_2           conda-forge
libgomp                      14.2.0           h77fa898_1           conda-forge
libiconv                     1.17             hd590300_2           conda-forge
libjpeg-turbo                3.0.0            hd590300_1           conda-forge
liblapack                    3.9.0            26_linux64_openblas  conda-forge
liblzma                      5.6.3            hb9d3cd8_1           conda-forge
libmpdec                     4.0.0            h4bc722e_0           conda-forge
libopenblas                  0.3.28           pthreads_h94d23a6_1  conda-forge
libpciaccess                 0.18             hd590300_0           conda-forge
libpng                       1.6.45           h943b412_0           conda-forge
libsqlite                    3.48.0           hee588c1_0           conda-forge
libstdcxx                    14.2.0           hc0a3c3a_1           conda-forge
libstdcxx-ng                 14.2.0           h4852527_1           conda-forge
libsvm                       335              h5888daf_1           conda-forge
libtiff                      4.7.0            hd9ff511_3           conda-forge
libuuid                      2.38.1           h0b41bf4_0           conda-forge
libwebp-base                 1.5.0            h851e524_0           conda-forge
libxcb                       1.17.0           h8a09558_0           conda-forge
libzlib                      1.3.1            hb9d3cd8_2           conda-forge
matplotlib-base              3.10.0           py313h129903b_0      conda-forge
mrcfile                      1.5.3            pyhd8ed1ab_1         conda-forge
munkres                      1.1.4            pyh9f0ad1d_0         conda-forge
ncurses                      6.5              h2d0b736_2           conda-forge
numpy                        2.2.2            py313h17eae1a_0      conda-forge
openjpeg                     2.5.3            h5fbd93e_0           conda-forge
openssl                      3.4.0            h7b32b05_1           conda-forge
packaging                    24.2             pyhd8ed1ab_2         conda-forge
pandas                       2.2.3            pypi_0               pypi
pcre2                        10.44            hba22ea6_2           conda-forge
pillow                       11.1.0           py313h8db990d_0      conda-forge
pip                          24.3.1           pyh145f28c_2         conda-forge
pixman                       0.44.2           h29eaf8c_0           conda-forge
psutil                       6.1.1            py313h536fd9c_0      conda-forge
pthread-stubs                0.4              hb9d3cd8_1002        conda-forge
pycairo                      1.27.0           py313h112314f_0      conda-forge
pycparser                    2.22             pyh29332c3_1         conda-forge
pymol                        3.1.0a0          pypi_0               pypi
pyparsing                    3.2.1            pyhd8ed1ab_0         conda-forge
pyqt5                        5.15.11          pypi_0               pypi
pyqt5-qt5                    5.15.16          pypi_0               pypi
pyqt5-sip                    12.16.1          pypi_0               pypi
pysocks                      1.7.1            pyha55dd90_7         conda-forge
python                       3.13.1           ha99a958_105_cp313   conda-forge
python-dateutil              2.9.0.post0      pyhff2d567_1         conda-forge
python_abi                   3.13             5_cp313              conda-forge
pytz                         2024.2           pypi_0               pypi
qhull                        2020.2           h434a139_5           conda-forge
readline                     8.2              h8228510_1           conda-forge
reportlab                    4.2.5            py313h536fd9c_0      conda-forge
requests                     2.32.3           pyhd8ed1ab_1         conda-forge
rlpycairo                    0.2.0            pyhd8ed1ab_0         conda-forge
scipy                        1.15.1           py313h750cbce_0      conda-forge
setuptools                   75.8.0           pypi_0               pypi
six                          1.17.0           pyhd8ed1ab_0         conda-forge
tk                           8.6.13           noxft_h4845f30_101   conda-forge
tzdata                       2025.1           pypi_0               pypi
unitment                     0.0.9            pypi_0               pypi
urllib3                      2.3.0            pyhd8ed1ab_0         conda-forge
xorg-libice                  1.1.2            hb9d3cd8_0           conda-forge
xorg-libsm                   1.2.5            he73a12e_0           conda-forge
xorg-libx11                  1.8.10           h4f16b4b_1           conda-forge
xorg-libxau                  1.0.12           hb9d3cd8_0           conda-forge
xorg-libxdamage              1.1.6            hb9d3cd8_0           conda-forge
xorg-libxdmcp                1.1.5            hb9d3cd8_0           conda-forge
xorg-libxext                 1.3.6            hb9d3cd8_0           conda-forge
xorg-libxfixes               6.0.1            hb9d3cd8_0           conda-forge
xorg-libxrender              0.9.12           hb9d3cd8_0           conda-forge
xorg-libxxf86vm              1.1.6            hb9d3cd8_0           conda-forge
zstandard                    0.23.0           py313h80202fe_1      conda-forge
zstd                         1.5.6            ha6fb4c9_0           conda-forge

Additional context

My pymol short cut does run through __init__.py

if [ ! -d ~/bin ]; then mkdir ~/bin; fi;
    echo '#!/bin/sh' > ~/bin/pymol
    echo exec \"/opt/.miniconda/envs/protein_pymol/bin/python\" \"$(( python -c "import site; print(site.getsitepackages()[0])" ) >&1)/pymol/__init__.py\" \"\$@\" >> ~/bin/pymol
    chmod 777 ~/bin/pymol

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions