Thonny path manager?

633 views
Skip to first unread message

Scott Richards

unread,
Nov 9, 2019, 8:49:25 AM11/9/19
to thonny
Hello,

I'm sure this has been asked many times, but the search terms I used didn't reveal relevant posts, so I'll ask:  What is the best way to manage (add/remove/re-order) the paths that Thonny uses to search for "import" modules? 

Reason:  I have Thonny installed on a company lab computer without internet access.  I would like to run some programs that use Numpy, Matplotlib, and several other packages that are not part of the standard Thonny installation.  I have installed Anaconda on that computer, which does have those package libraries, but I haven't been able to get Thonny to search the Anaconda library paths when trying to "import" modules.

Fyi, I'm an engineer who isn't a software developer but who still writes many Python scripts to interface with test equipment, collect data, run automated tests, display results, etc..  I suspect that I represent the majority of Python users (by head count, not by lines of code.)  I have found that the Thonny IDE has the best balance of capability vs. simplicity.  Fantastic work!

Aivar Annamaa

unread,
Nov 9, 2019, 9:33:25 AM11/9/19
to thonny
Hi Scott,

I believe Anaconda's libraries are superset of Thonny's, therefore I'd recommend you to swap out the whole back-end interpreter: Tools => Options => Intepreter => select Anaconda's python.

best regads,
Aivar

Scott Richards

unread,
Nov 9, 2019, 12:00:23 PM11/9/19
to thonny
Mr. Annamaa,
First, thanks for the very fast response.  Second, I did try that, and I thought it would work, but it didn't.  Thonny definitely acknowledged that it was using Anaconda's python interpreter, but for some reason, it still wouldn't import the Anaconda modules.  The modules are definitely there because Spyder can import them.

Is it possible that I installed Thonny in a place where it can't "see" the Anaconda paths, even though it is using the Anaconda interpreter?  I installed Thonny in \Anaconda\Scripts.  Should I have installed Thonny in a folder "above" Anaconda?

Kind Regards,
Scott

Scott Richards

unread,
Nov 9, 2019, 12:15:11 PM11/9/19
to thonny
Edit:  I just found:  https://github.com/thonny/thonny/wiki/InstallingPackages

In that explanation, you indicate that Anaconda's interpreter would be "pythonw.exe".  I'm not sure what the difference is between "python.exe" and "pythonw.exe" but I'll change the interpreter to that and see what happens.  If it works, I'll post another update.

Aivar Annamaa

unread,
Nov 9, 2019, 12:32:11 PM11/9/19
to thonny
Sorry, pythonw.exe on this wiki page was a leftover from a previous solution. python.exe is correct.

How did you install Thonny? If you pip-installed into Anaconda, then the default intepreter ("Same as front-end") should do fine. 

What does following print if you execute it in Thonny:

import sys
print(sys.executable)
print(sys.path)

Best regards,
Aivar

Scott Richards

unread,
Nov 9, 2019, 1:05:35 PM11/9/19
to thonny
I installed Thonny on the computer just as I would have installed it if Anaconda weren't already installed.  When the Thonny installer asked me for a location to install Thonny, I selected \Anaconda\Scripts\.  I don't have access to the computer for a few more days, but I'll try the following in order:

1.  Find the sys.executable and sys.path variables from both Thonny and Spyder.  If the sys.executable is different, I'll change the Thonny one to match the Spyder one.
2.  If the executables match, but the path lists are different, I'll uninstall Thonny and "pip" or "conda" install Thonny from Anaconda.  

I'll let you know what works.

Scott Richards

unread,
Nov 11, 2019, 9:39:31 AM11/11/19
to thonny
Neither of the two solutions worked.

1.  Even if I change the Thonny interpreter to be the same as the one used by spyder (found via sys.executable) AND change the sys.path variable in Thonny to include all the same paths as those used by spyder, I couldn't import numpy.
2.  I could not seem to pip install the Thonny ".whl" file.  The pip installer seemed to be looking for a dependency that it couldn't find because that computer has no access to the internet.

The problem of not being able to find packages seems to be related to creating and activating "virtual environments".  Virtual environments, as I understand them, are useful for development of software that is much larger than anything I'm building.  I'm just writing programs to control test equipment, gather test data, analyze and reduce the data, and then plot the data.  So I have no reason to restrict the modules that my programs have access to.

SOLUTION:  What DID work was to uninstall Anaconda and re-install it while checking the "Add Anaconda to my PATH variable" box (the one that the installer warns you against selecting.)  THEN, after changing the Thonny python interpreter to the one in the Anaconda directory, any of the Anaconda modules could be imported.

I hope this helps someone else who (like me) probably saw "virtual environments" discussed in a dozen other places but ignored it thinking that it didn't apply to my case.

Aivar Annamaa

unread,
Nov 12, 2019, 2:33:41 AM11/12/19
to tho...@googlegroups.com

I suspect you were bitten by an Anaconda bug: https://github.com/ContinuumIO/anaconda-issues/issues/10884 and your solution of adding Anaconda to PATH was the proper workaround.

Best regards,
Aivar

--
You received this message because you are subscribed to the Google Groups "thonny" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thonny+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/thonny/8db6f525-f4c7-4685-8fd7-3985dd7b41c4%40googlegroups.com.

Scott Richards

unread,
Nov 12, 2019, 6:05:07 AM11/12/19
to thonny
On this side, everything is working well and there were useful knowledge gains of Thonny, Anaconda, and Python. I/we sincerely appreciate your efforts in creating and improving this excellent IDE, and also your taking the time to help with this issue. Thanks again. - Scott

Stanley Krasnow

unread,
Nov 12, 2019, 8:07:50 AM11/12/19
to thonny
I completely agree with Scott RIchards; Thonny is a perfect fit for my use as a retired business person trying to reconnect with my technical roots (and automate our house).  Thank you for your creating and improving this excellent IDE.  I have one question - is there a simple, straight-forward way to add a library of modules I've written to the Thonny Python Path?  Currently I include this code in each new script:
    import sys    
    libPath = '/Library/Python/2.7/site-packages'
    if not libPath in sys.path: sys.path.append(libPath)

I would like to be able to avoid the above three lines.  I am on OSX Catalina.   Thank you.

Stan
To unsubscribe from this group and stop receiving emails from it, send an email to tho...@googlegroups.com.

Aivar Annamaa

unread,
Nov 12, 2019, 8:32:36 AM11/12/19
to thonny
Hi Stan,

You could store your modules in "user site packages" directory, which is on Python path by default. You can learn the location of this directory like this:

import site
print(site.getusersitepackages())

This is the same directory that is used when you `pip install --user <package>`. Also, Thonny's package manager installs there. 
If the directory doesn't exist yet, then it is not on the path yet -- just create it yourself and restart Thonny's back-end to make it appear in the path.

Alternatively, you could run Thonny with a PYTHONPATH environment variable set to your desired location. 

Best regards,
Aivar

myep...@gmail.com

unread,
Aug 19, 2020, 5:14:08 PM8/19/20
to thonny
Thanks for your excellent work in Thonny. I'm using it to teach my grandchildren to code and do software engineering.

Running Thonny 3.2.7 on Windows 7 64-bit, I've set PYTHONPATH environment variable, but it doesn't make it into the sys.path in Thonny.
I've got the interpreter set to 3.8.5 in Thonny.
If I open a non-Thonny cmd window and run python from there, sys.path includes PYTHONPATH.
Also, when I click 'system shell' from Thonny Tools menu, the interpreter used is the one installed with Thonny, not the 3.8.5 that I've specified.
No support for PYTHONPATH is inconvenient because I'm upgrading/fixing some standard packages, and it is much easier to do that work in
my own directories (rather than site-packages directory). In each module, I have to do a sys.path.append(...) to point to my work area.

Also, have you considered saving more state of last Thonny session and opening new session based on that? For instance, it is a lot of work to open
all of the files that were opened in the last Thonny session. Alternately, the notion of a project which could contain multiple files would serve the same purpose.

Thanks for your help,
David

Aivar Annamaa

unread,
Aug 20, 2020, 10:52:57 AM8/20/20
to thonny
Hi David!

Thonny explicitly removes PYTHONPATH environment variable before starting the process for user code, because several users used to report problems, where another program had set this variable globally and the libraries pointed by it were not compatible with Thonny's interpreter. I prefer to keep this "clean slate" approach, but I added a task to my TODO list for allowing custom environment variables to set (https://github.com/thonny/thonny/issues/1308)

> Also, when I click 'system shell' from Thonny Tools menu, the interpreter used is the one installed with Thonny, not the 3.8.5 that I've specified

This sounds like a bug. Can you please send me the header that Thonny prints when the system shell is opened? If your 3.8.5 is not mentioned, then please send its location as well.

> Also, have you considered saving more state of last Thonny session and opening new session based on that?  For instance, it is a lot of work to open
all of the files that were opened in the last Thonny session.

You should check "Tools => Options => General => Reopen all files from previous session". Have you tried Thonny's file browser (View => Files)? I prefer using this for opening my files.

> Alternately, the notion of a project which could contain multiple files would serve the same purpose.

I prefer not to introduce another IDE-specific project format. If there appears a standard for this, then I will make Thonny support it.

Best regards,
Aivar
Reply all
Reply to author
Forward
0 new messages