6

I feel like this is a basic question, so feel free to direct me to any resources:

My conda environment uses .local ahead of the package version specified in the yaml file for any package that exists in local. How do I get it to ignore .local, if that's possible?

I'm using PyCharm and Ubuntu.

If I can provide more information, let me know.

feetwet
  • 3,248
  • 7
  • 46
  • 84
Python Developer
  • 551
  • 1
  • 8
  • 18

1 Answers1

8

I think all python interpreters will use packages from site.USER_SITE before any others, and by default that location is ~/.local/lib/pythonX.Y/site-packages. That's because site.USER_BASE defaults to ~/.local.

But fortunately, you can override site.USER_BASE to some other value with an environment variable: PYTHONUSERBASE. Since you want to disable it entirely, you should supply a non-empty nonsense value. For example:

$ export PYTHONUSERBASE=intentionally-disabled
$ python -c "import site; print(site.USER_SITE)"
intentionally-disabled/lib/python3.7/site-packages

Docs:

To make sure that variable is set every time you're using that conda environment, you can create a post-activation shell script in ${CONDA_PREFIX}/etc/conda/activate.d/, as explained in this answer.

cat > ${CONDA_PREFIX}/etc/conda/activate.d/disable-PYTHONUSERBASE.sh << EOF
#!/bin/bash
export PYTHONUSERBASE=intentionally-disabled
EOF

chmod +x ${CONDA_PREFIX}/etc/conda/activate.d/disable-PYTHONUSERBASE.sh

But frankly, I think the simplest option is to never use ~/.local for python packages. Just move or delete them. It causes issues like this. I've only encountered it when its causing problems -- I've never seen anyone actually benefit from using that Python feature. I wish they would just disable it by default.


Edit: If your IDE allows you to specify the flags that are passed to python itself, then you can use the python -s option.

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • thanks for the great answer! i implemented your solution but also needed to add PYTHONUSERBASE as a user defined environment variable in my PyCharm run configurations because they didn't have PYTHONUSERBASE even though the terminal for the conda environment did. you don't recommend deleting `~/.local`, right? just deleting the packages installed in it. i imagine deleting the folder might cause problems. – Python Developer Jun 15 '20 at 03:07
  • 1
    Hmm, yeah, I guess that's best. I don't really know what `~/.local` is for, TBH. [This answer](https://askubuntu.com/questions/14535/whats-the-local-folder-for-in-my-home-directory) claims that it's for more than just Python. – Stuart Berg Jun 15 '20 at 03:13
  • I guess the cleaner solution is to set `PYTHONNOUSERSITE` (see ), which has the same effect as `python -s` (), instead of setting `PYTHONUSERBASE` to some nonsense value. – Stefan Apr 17 '23 at 15:50