2

I am a bit new to using the command line, so I apologize if this question is rather basic. I believe a have installed a module called lmfit using

pip3 install lmfit

and it says this was successful. However, I still get a

ModuleNotFoundError: No module named 'lmfit'

when I try to import lmfit in a python script. I've tried to check if it was really installed using

pip3 show lmfit

and this gives the location of the module in a folder called python3.8 on my local computer.

Any advice would be appreciated!

  • Can you check which version of python you are you running before you are importing the module? import sys sys.version – Zenquiorra Jun 15 '20 at 09:01
  • Also, please check the output of sys.path sys.executable It could be possible that python is executed in some environment and the module is installed in a different environment. – Zenquiorra Jun 15 '20 at 09:04
  • @Zenquiorra Thanks! I get 'command import not found' for the first command, and also 'command not found' for all of sys,version, sys.path, and sys.executable. Unless you mean I should run these in python, not the command line? – ellie_arroway Jun 15 '20 at 18:32
  • @Zenquiorra Oh wait, I see I should run them in python. sys.version gives 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] sys.path gives a bunch of path names, and I don't know what I should be looking for. sys.executable gives a file called pythonw.exe in my folder Anaconda3. – ellie_arroway Jun 15 '20 at 18:34
  • Ok found the error. A possible fix will depend on your next response! The error is: You are running python 3.7 and the module is installed for python 3.8.

    Can you please paste the output of python --version and python3 --version. Also, how are you running python on your system? Are you using python command or python3 from the CLI? If you are using python, then can you please try running python3 and importing the module there?

    – Zenquiorra Jun 16 '20 at 07:57
  • @Zenquiorra The output of python --version is ~$ python version Command 'python' not found, did you mean: command 'python3' from deb python3 command 'python' from deb python-is-python3 The output of python3 --version is Python 3.8.2 – ellie_arroway Jun 16 '20 at 17:22
  • I am running Python using Spyder. However, I can also run using a Jupyter notebook in Google colab which works just fine after pip3 install lmfit. But I still want to make it work in Spyder for future use. – ellie_arroway Jun 16 '20 at 17:25
  • I am not running it form the command line, but simply clicking on the Spyder icon, I'm still getting used to the command line :) – ellie_arroway Jun 16 '20 at 17:26
  • If the answer worked then please accept it for others users to try the solution. If not, then share the error here in comments, I will modify my solution accordingly. It is recommended that you straight away try point 3 (regarding spyder) – Zenquiorra Jun 17 '20 at 05:47

1 Answers1

0

It seems that you have the lmfit module installed for python 3.8 and you are running python 3.7 on Spyder for which the module is not installed.

Preferrable method for you is option 3 as you said you want to fix it for Spyder. The blockquote explains the detailed procedure to do that.

  1. A possible fix is to change the symbolic link and fix it to python 3.8. You can check how to change the path here (Follow the instructions and replace 3.6 with 3.7 and 3.7 with 3.8 for your case)
  2. Use a virtual environment to juggle between multiple versions.
  3. Change the version which spyder uses for python. To do this:

Open Preferences in Spyder => Tools - Preferences

Click on Python Interpreter from the options in left

Now the Selected option, right below Select the Interpreter for all Python Consoles should be Default (i.e. use the same as Sypder's), Now change it to Use the following interpreter: and it will ask for a path below it.

Provide the path to your Python 3.8 installation folder. You can find the path using:

whereis python3.8 in your ubuntu command line interface (Note for windows where python3.8 should work fine).

In general the path should be something like \usr\bin\python3.8 but it depends on how you have installed it. Take the path to python3.8 and paste it in that path. You can also browse for python3.8 in that path and select it manually from the spyder interface.

Now Apply it and Ok it. Restart Spyder and you should be running python3.8 and now you can import lmfit in there.

A visual representation of this process (except on how to find the path) could be found here

  • Thanks @Zenquiorra. When I try to get the path using whereis python3.8, it outputs a bunch of file paths. This includes the one you suggest, /usr/bin/python3.8, but when I enter that path I get an error from the Python interpreter: 'Invalid file path:`. – ellie_arroway Jun 17 '20 at 18:22
  • Can you please try going towards this path cd ~ and then navigating towards usr and then the remaining path. You can most probably find it in home after going to ~. So it should be cd ~ then cd home -> cd usr and ... But it can vary. Try locating usr by whereis usr from the path ~. And then paste this path to python3.8 by adding the prefix path to it from ~ On the other hand, you also said : this gives the location of the module in a folder called python3.8 on my local computers Can you please use this path to locate python3.8 and input it in the prompt? – Zenquiorra Jun 19 '20 at 13:01
  • It is suggested if typing in/ pasting the path does not work, then you can browse for python 3.8 in the related path from the prompt and manually select it. Also, you open the file explorer(nautilus) and search for python3.8 in /usr/bin (most probably in home), after you have found pytho3.8, you can right click in the file explorer and there should be an option to Open in Terminal. Click on that and you can find the whole path to that folder in terminal. You can copy that and add python3.8 to it. – Zenquiorra Jun 19 '20 at 13:06
  • Given that you have found python3.8 through file explorer, If the option to Open in Terminal does not show up after you right click, then you can simply add it by 2-3 commands using this.: link

    The idea here is you have to provide a global path to python3.8 in Spyder. So we are trying to find a global path, relative to ~

    – Zenquiorra Jun 19 '20 at 13:08