Spyder: Set the path of Spyder Configuration Folder

Created on 16 Feb 2017  路  21Comments  路  Source: spyder-ide/spyder

Description of your problem

Currently Spyder creates and use Configuration Folder is resides in the User Home Folder.
The user home folder is usually a path defined by an environment variable.

The problem is sometimes a Portable operation mode is needed.
Portable Operation Mode means no data is saved outside the installation folder of Spyder (Including registry keys for that matter).

What steps will reproduce the problem?

  1. Run Spyder (Folder is created at first run).

What is the expected output? What do you see instead?

I'd like to be able to set Spyder to portable mode where it creates and uses a configuration folder within its installation folder.

Thank You.

Preferences Enhancement future

Most helpful comment

Ok, we'll add it in Spyder 3.2 :-)

All 21 comments

@stonebig, what do you think about this one? Would it be useful for WinPython?

@stonebig , @ccordoba12 We talked about it here - https://github.com/winpython/winpython/issues/381#issuecomment-243855502.

Hi @ccordoba12

It would be nice. I didn't look how Anaconda manages that, if it does

Ok. I think adding a command line option to set Spyder's directory to save its config is the right thing to do. Do you agree?

It looks like a simple solution. for me it's ok

Ok, we'll add it in Spyder 3.2 :-)

@ccordoba12 , That's Great!

By the way, does Spyder also the one creating a .QT5 Folder?

Nop, we don't have anything to do with that.

Closing because we don't have time for this, sorry.

Can't it be left open for next version?

We're not going to work on this, so it doesn't make sense to leave it open.

Reopening due to popular demand.

Until it becomes a feature, is there any "hacky" way to change the path of the spyder.ini file at this time? Or better still the whole .spyder-py3 directory?

This would be useful to keep multiple copies of Spyder in different environments. I'm finding it difficult to remember when I'm working in my "normal" environment and when I'm in the "experimental" one, so I was thinking of setting a dark theme in one and a light theme in the other. That and a few other customizations.

editing get_home_dir() in /spyder/config/base.py as below seems to work for me, i havent tested much though, so it might break something somewhere else

def get_home_dir():
    """Return user home directory."""
+    path = os.environ.get("SPYDERHOME")    
+    if osp.isdir(path):
+        return path
    try:
        # expanduser() returns a raw byte string which needs to be
        # decoded with the codec that the OS is using to represent
        # file paths.
        path = encoding.to_unicode_from_fs(osp.expanduser('~'))
    except Exception:
        path = ''

    if osp.isdir(path):
        return path
    else:
        # Get home from alternative locations

@ptocca so for now we could add this option on the CLI for cases where multiple instances of Spyder are opened. I guess for 4.2

Thanks for your reactivity! Sounds great! :-)

What is "SPYDERHOME"? your own variable to store the PATH to Spyder? (I didn't see it defined anywhere in the code). A few lines up inside def get_conf_subfolder(): , the logic is spelled out clearly on line 131. Quote:

# We can't have PY2 and PY3 settings in the same dir because:
# 1. This leads to ugly crashes and freezes (e.g. by trying to
#    embed a PY2 interpreter in PY3)
# 2. We need to save the list of installed modules (for code
#    completion) separately for each version

https://github.com/spyder-ide/spyder/blob/00ca2272715e55b7947ba4d2354fd1dbee25d4fa/spyder/config/base.py#L131

Maybe the lines:

if PY3:
    SUBFOLDER = SUBFOLDER + '-py3'

could be followed by something like:

if MYENV:
    SUBFOLDER = osp.join(SUBFOLDER, 'envs', MYENV)

assuming MYENV is None when Spyder is in the root environment and MYENV is 'myenv' (the name of the environment) when Spyder is in the environment.

What is "SPYDERHOME"? your own variable to store the PATH to Spyder? (I didn't see it defined anywhere in the code). A few lines up inside def get_conf_subfolder(): , the logic is spelled out clearly on line 131. Quote:

In the example SPYDERHOME is just an env variable to replace the user's home path as the root of the config, in my case, at work the home path gets wiped on every login taking away the config with it otherwise
It wouldn't help with different configs for different environments, for that i guess you'd have to modify the def get_conf_subfolder() with something like what you wrote

I get it saucoide, thanks. So how do you actually proceed? Do you compile Spyder after you've made the edit to the directory? Or are you able to make the edit it after Spyder is already installed (how)?

I just overwrite that file after installing spyder on the env i'm using (which also gets wiped every time, unfortunately), it's a plaintext python file so no need to do anything special i think

Thanks! I found the file here:

~/miniconda3/pkgs/spyder-4.1.3-py38_0/lib/python3.8/site-packages/spyder/config/base.py

Getting the path to the environment I can set the config directory in get_conf_subfolder() as follows:

from spyder.utils.misc import get_python_executable 
from spyder.plugins.ipythonconsole.utils.kernelspec import is_different_interpreter
pyexec = get_python_executable()
conda_env = os.path.dirname(os.path.dirname(pyexec))
if is_different_interpreter(pyexec):
    SUBFOLDER = os.path.join(SUBFOLDER, 'envs', conda_env)

Cool!

Remark in passing: The code sometimes imports os.path as osp and sometimes doesn't.

image
Even my miniconda has same issue by default the spyder4 installed is meddling in files .spyder-py3 related to my base python spyder. It seems that because of this base spyder is throwing runfile error

Was this page helpful?
0 / 5 - 0 ratings