Setting/Exporting LD_LIBRARY_PATH on launch of WSL

Answered

I'm trying to use a module compiled as part of a linked library, so it needs to link to the pieces when importing the module.

To do this, the paths must be in LD_LIBRARY_PATH, but I just can't seem to put them in there on the launch of the shell.

So far I have tried:

Connecting over SSH, and exporting LD_LIBRARY_PATH via .bashrc and .ssh/environment

Using native WSL and setting the LD_LIB... to the paths I need wherever I can.

I see that the command used to launch the WSL shell is

"C:\Users\{$USER$}\AppData\Local\Microsoft\WindowsApps\ubuntu1804.exe run "export PYTHONUNBUFFERED=1 && ... "

If I could simply add my export LD_LIB... command here, it would be perfect.

Is there any way to do that? Or any way to set LD_LIBRARY_PATH in WSL shells without having to do a custom run config for everything?

Thank you for your help

UPDATE: The Python console has Environmental Variable settings, which I was able to use to set LD_LIB...

In PyCharm 2018.3 the setting path is Settings > Build, Execution, Deployment > Console > Python Console > Environmental Variables

I can now use the Python Console, but I still cannot get code insight through the interpreter as the interpreter cannot link properly.

Reformed Question: Is there a way to change the environmental variables (apart from PYTHONPATH) of the interpreter when it launches? I am using the WSL "native" interpreter.

7 comments

Hi,

I think what you are looking for is configuration templates. If you open Run -> Edit Configurations -> Templates, you can add your environment variable to Python template. Then, every configuration automatically created from that template will include this variable.

Please try it and tell us if that's what you were looking for?

1

Thank you for your response!

Run configurations are important to this workflow, and have definitely given me some trouble before, so thank you for adding that note.

That's not the particular issue I'm having right now. I've updated the question to include my slimmed down issue.

Right now, I can't get code introspection on my module because the interpreter can't resolve the import because it can't find the right paths in LD_LIB...

Is there a way to change LD_LIB, or environmental variables in general, for the interpreter?

My interpreter is "native" WSL Ubuntu 18.04

0

Hi Matt,

Answering your updated question
> Is there a way to change the environmental variables (apart from PYTHONPATH) of the interpreter when it launches?

This can be done in run configuration as mentioned in my previous post. You can set any env. variable there which will be used during the script execution.

 

Regarding your issue with code insight. Please try adding all directories containing the modules and libraries that you use as content roots in Settings | Project: <name> | Project Structure

More information here:

https://www.jetbrains.com/help/pycharm/project-structure-dialog.html

Be sure to mark those directories as Sources. This should tell IDE where to look for your custom modules, and they should be resolved and included in code insight.

If this isn't very helpful for you, please give more details about your project structure and what exactly in code insight is working/not working

1

Hi Andrey,

Thank you again for responding.

Your solution for the run configuration works for me.

For the code insight, I have done what you recommended previously, and it did not solve my issue. The import function is still underlined, and cannot find the module, so no code insight is functioning. The module I'm trying to import is a boost python compiled object, with links to other .so files. The directory can be viewed as such

Project Directory/

    bin/

        lib/

            part1.so

            part2.so

            python-module.so

    other_so/

        lib/

            tp1.so

            tp2.so

The python module is compiled with links to the other .so files in both directories. I've added both bin/lib and other_so/lib as source dirs, but the import still isn't working.

As a side note, when I'm using the python console, code insight and autocomplete are available for the module, as I can successfully import it.

I suspect that the mechanism for code insight needs to be able to link the module to the other .so files, so it needs LD_LIBRARY_PATH set somewhere.

0
Hi Matt, I have the additional information regarding how to link external libraries to PyCharm. In general, there are two main ways to do that: 1. Add the directories containing the libraries into your PYTHONPATH environment variable and restart PyCharm. PyCharm is not using LD_LIBRRARY_PATH but it should scan all directories in PYTHONPATH and find all the libraries there. 2. In Project Interpreter configuration, add the library directories to your Interpreter Paths. Please see the screenshot: http://bit.ly/2SGSRnZ I suggest to try both options. Keep in mind you have to restart PyCharm in order for it to re-scan the directories. Please let me know how it goes.
0

Hi Jiri,

I have done both of these, and neither of them produce the result of code introspection for PyCharm. PyCharm still cannot find the module I am attempting to import. I think that the issue is not that it can't find the module, but that it can't resolve its linking.

However, the python console (launched within pycharm) has no problems with this, and it finds the module i'm trying to import and gives code insight, and also does not use LD_LIBRARY_PATH. The additional step here for the console seems to be that it extends the sys.path, which might make the background linker able to resolve the library links. Is there any way to do something similar for the interpreter?

0

The trick with Interpreter Paths should work indeed (both in runtime and in the editor), the problem is PyCharm seems to have a bug in WSL support resulting in custom path ignoring. I created a ticket in PyCharm's bug tracker to deal with this issue: PY-33295.

0

Please sign in to leave a comment.