bruderstein / PythonScript

Python Script plugin for Notepad++
http://npppythonscript.sourceforge.net/
GNU General Public License v2.0
355 stars 66 forks source link

Sys.Path (still) missing some directories in latest 3.0.17 version #322

Closed vinsworldcom closed 7 months ago

vinsworldcom commented 10 months ago
          @chcg it's better, but now not finding the PythonScript site directory:
'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\lib'
 'C:\\usr\\bin\\npp64\\plugins\\Config\\PythonScript\\lib'
 'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\scripts'
 'C:\\usr\\bin\\npp64\\plugins\\Config\\PythonScript\\scripts'
 'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\lib\\lib-tk'
 'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\python312.zip'
 'C:\\usr\\bin\\npp64\\plugins\\PythonScript'
 'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\Lib'
 'C:\\usr\\bin\\npp64']

Notice :

'C:\usr\bin\npp64\plugins\PythonScript\lib\site-packages

is missing and it was there before (see previous posts).

Also, now it's not finding any of the local system Python paths. Note I do not have the "Prefer installed Python libraries ..." checked, but the previous versions always found the local system Python paths:

'C:\Users\VinsWorldcom\AppData\Roaming\Python\Python3xx\site-packages\**'

This new commit is no longer finding them, and as you can see from above, the "original" 3.0.17 did find the local system Python paths.

At the very least, if it imports the local PythonScript site packages:

plugins\PythonScript\lib\site-packages

Then I can put a .pth file in that directory with the paths of my system Python installation site files and get the functionality I want.

Cheers.

Originally posted by @vinsworldcom in https://github.com/bruderstein/PythonScript/issues/321#issuecomment-1880215781

dinkumoil commented 7 months ago

I'm facing an issue that could be related to the one here. I use a portable version of Notepad++ v8.6.4 and PythonScript plugin. Until plugin version 3.0.16 everything is fine. Starting with v3.0.17 I get an error message in plugin's console when starting up Notepad++.

Under <Npp-BasePath>\plugins\config\PythonScript\scripts\startup.py I have a user startup script that contains the following lines:

from Utils.PreProcessorLexer import PreProcessorLexer
pre_processor_lexer_interface = PreProcessorLexer()

The module I want to import is <Npp-BasePath>\plugins\config\PythonScript\scripts\Utils\PreProcessorLexer.py. However, the error message mentioned above tells me ModuleNotFoundError: No module named 'Utils', thus PreProcessorLexer.py is not loaded. As already said, this misbehavior started with PythonScript plugin v3.0.17, with v3.0.16 everything works.

Since my knowledge of Python is very limited, I'm not sure if this is related to the issue at hand. Maybe someone more knowledgeable could share their thoughts.

Additional info: <Npp-BasePath>\plugins\config\PythonScript\scripts\Utils does not contain a file named __init__py, in case that would be relevant.

vinsworldcom commented 7 months ago

@dinkumoil almost certainly the same issue I describe. No fix yet.

mpheath commented 7 months ago

@vinsworldcom I have tried this artifact .

Looks like site config changed from enabled to disabled https://github.com/bruderstein/PythonScript/commit/f4b6f0241b68cdf00cc2a00026ba62b9fe7b19ef#diff-9270932fd4a7a300cabd17a1f0462588f5111832df6302c3009a56fdccc5f7eb

https://github.com/bruderstein/PythonScript/blob/6851859fc1bfa043beb92d232483361122f1d477/PythonScript/src/PythonHandler.cpp#L97-L102

Check value in PythonScipt sys.flags.no_site which v3.0.17 displays 0 and the artifact displays 1. In C++ source code Py_NoSiteFlag = 1; and config.site_import = 0; are not equivalent including the other 2 config lines.

Output from `print()` in workaround code with the initial work around commented out. v3.0.17: ``` sys.flags.no_site: 0 sys.flags.ignore_environment: 0 sys.flags.no_user_site: 0 site.ENABLE_USER_SITE: True site.USER_BASE: C:\Users\Michael\AppData\Roaming\Python site.USER_SITE: C:\Users\Michael\AppData\Roaming\Python\Python312\site-packages ``` Artifact: ``` sys.flags.no_site: 1 sys.flags.ignore_environment: 1 sys.flags.no_user_site: 1 site.ENABLE_USER_SITE: None site.USER_BASE: None site.USER_SITE: None ```

For AppData user paths, you could try to add to start.py as a workaround with the artifact

```py import site site.ENABLE_USER_SITE = True site.main() print('sys.flags.no_site:', sys.flags.no_site) print('sys.flags.ignore_environment:', sys.flags.ignore_environment) print('sys.flags.no_user_site:', sys.flags.no_user_site) print('site.ENABLE_USER_SITE:', site.ENABLE_USER_SITE) print('site.USER_BASE:', site.USER_BASE) print('site.USER_SITE:', site.USER_SITE) ``` to force user site paths to be added to `sys.path`. I created a test path in AppData to site-packages and added a `foo` folder with `__init.py__` file inside. `import foo` causes no error. List from `sys.path`: ``` C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript\\lib C:\\Programs\\Notepad++\\8.6.5\\plugins\\Config\\PythonScript\\lib C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript\\scripts C:\\Programs\\Notepad++\\8.6.5\\plugins\\Config\\PythonScript\\scripts C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript\\lib\\lib-tk C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript\\python312.zip C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript C:\\Programs\\Notepad++\\8.6.5 C:\\Users\\Michael\\AppData\\Roaming\\Python\\Python312\\site-packages C:\\Programs\\Notepad++\\8.6.5\\plugins\\PythonScript\\Lib\\site-packages ``` Still `1` for `sys.flags` ... though `site` sets user paths. Weather the `sys.flags` still at `1` messes up the running, IDK. ``` sys.flags.no_site: 1 sys.flags.ignore_environment: 1 sys.flags.no_user_site: 1 site.ENABLE_USER_SITE: True site.USER_BASE: C:\Users\Michael\AppData\Roaming\Python site.USER_SITE: C:\Users\Michael\AppData\Roaming\Python\Python312\site-packages ``` Quite a bad hack though at the moment, might be better then the released v3.0.17 (no guarantee). If not worth it, use what ever version that works for you. I have not checked `win32gui` to know if that works.

@dinkumoil No scripts dir in sys.path of released v3.0.17 so Utils not found by import statement. The artifact might be better else probably go back to v3.0.16 if worked for you.

vinsworldcom commented 7 months ago

For AppData user paths, you could try to add to start.py as a workaround with the artifact

YES! I can and it works fine with my win32gui issue mentioned here.

Currently using:

### START: Deal with 3.0.17 sys.path/site bug: https://github.com/bruderstein/PythonScript/issues/322
import site
site.ENABLE_USER_SITE = True
site.main()
paths = [
    'C:\\Users\\VinsWorldcom\\AppData\\Roaming\\Python\\Python312\\site-packages',
    'C:\\Users\\VinsWorldcom\\AppData\\Roaming\\Python\\Python312\\site-packages\\win32',
    'C:\\Users\\VinsWorldcom\\AppData\\Roaming\\Python\\Python312\\site-packages\\win32\\lib',
    'C:\\Users\\VinsWorldcom\\AppData\\Roaming\\Python\\Python312\\site-packages\\Pythonwin',
    'C:\\usr\\bin\\npp64\\plugins\\PythonScript\\Lib\\site-packages',
]
for path in paths:
    if path not in sys.path:
        sys.path.append(path)
### END: Deal with 3.0.17 sys.path/site bug

Cheers.

chcg commented 7 months ago

@vinsworldcom @dinkumoil Could you please test with artifacts from https://ci.appveyor.com/project/bruderstein/pythonscript/builds/49581044 if with the change https://github.com/bruderstein/PythonScript/commit/0fc150ccfc419cba985bb04c1a450e573322a4dd behaviour is working again as with 3.0.16.

vinsworldcom commented 7 months ago

@chcg The artifact at https://github.com/bruderstein/PythonScript/commit/0fc150ccfc419cba985bb04c1a450e573322a4dd seems to have fixed it for me! Using my original 'startup.py' without the fix I mentioned above works as previously with 3.0.16.

Thank you!!

dinkumoil commented 7 months ago

@chcg I can confirm that the artifact fixed the issue. Thank you!

chcg commented 7 months ago

Fixed with https://github.com/bruderstein/PythonScript/releases/tag/v3.0.18