mandeep/sublime-text-conda

Importing numpy in activated conda environments seems broken with ST 4107

ARodenboog opened this issue · 7 comments

I recently unknowingly updated from Sublime Text 3 to Sublime Text 4. When I had done so, I could no longer execute files using the build command with the Conda package. After activating an environment, when running code that required numpy I got the famous import error : https://numpy.org/devdocs/user/troubleshooting-importerror.html

Without importing numpy (implicitely or explicitely) there was no issue, and when returning to Sublime Text 3 the issue disappeared as well.

If I remember correctly, this error can occur if the activated environment does not get added to the path, as this error was precisely the reason I started using the Conda package in the first place.

Thanks for reporting this. I'll update to ST4 and see if I can replicate the issue.

Not sure for what it's worth but I'm also experiencing this issue when upgrading to ST4

I am experiencing the same issue as well. @ARodenboog, have you recently updated anaconda as well?

While using previous version (not sure which) of Anaconda, I was able to import numpy in the python shell even after deactivating the environment in 'anaconda prompt'. After the update though, it gives the same import error which is seen while using this package on ST4. Maybe the package is not able to properly activate the environment after updating to ST4 and/or Anaconda update?

For now, I was able to get around it by creating a user defined build as follows:

{
    "cmd": ["C:\\Users\\user\\anaconda3\\Scripts\\activate.bat", "C:\\Users\\user\\anaconda3", "&&", "python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"},
    "cancel": {"kill": true}
}

Comparing it with the Conda.sublime-build file, "target" was removed and "cmd" was written to replicate the target behavior of the "Anaconda Prompt" shortcut from start menu in creating this build.

I'm using:
Win 10
ST4: Build 4113
Anaconda installer version: 2021.05, running conda version 4.10.3

Hope this helps.

Thanks for letting me know. It's possible the Windows build needs to be updated to reflect activate.bat. Hopefully I can get to it this week.

@mandeep I believe this issue persists if PATH variable was not set during Anaconda installation. Recently I installed anaconda on another system by selecting path variables and the package seemed to work as expected.

Following variables were set by the installer:
C:\Users\user_name\anaconda3;
C:\Users\user_name\anaconda3\Library\mingw-w64\bin;
C:\Users\user_name\anaconda3\Library\usr\bin;
C:\Users\user_name\anaconda3\Library\bin;
C:\Users\user_name\anaconda3\Scripts;

I tried setting only the first path to the variable of the previous system. However, that did not work. But once I set all the paths to the variable, it worked.

@rajas2211 So, the problem is that this results in odd behavior you'd see python fallback to those, breaking the point of the separate venvs to begin with. The st-conda package seems to emulate what conda activate does by sticking the respective environments at the front of your path.

@mandeep
I've tried to take an independent look at this issue and it's pretty odd. For some odd reason, despite your use of the ExecuteCondaEnvironmentCommand context manager to append the venv paths to the PATH enviromental variable, this doesn't seem to be passed through to the run_window calls within,

A test extracting the environment variable from within the context manager shows the following

self.window.run_command('exec', {'file_regex': '^[ ]*File "(...*?)", line ([0-9]*)', 'cmd': ['echo', '%PATH%'], 'env': {'PYTHONIOENCODING': 'utf-8'}, 'shell': True})
>>> $PATH_NOT_INCLUDING_ENV_PREFIXES

I'd like to help fix this, but I'm a bit stuck as to what might be causing this. If you'd be able to point me in the right direction here, I'd gladly take the time to make a pull request for this fix.

Hi @Klim314 thanks for taking the time to look into this. I'm not sure why the environment variable is not being set correctly so I'll have to ask the sublime folks.

Also, it seems that I am required to pay for a new license for ST4 as my ST3 license has expired. I'm not sure I care to buy another license so I may be very delayed in attacking this issue.