Python: importing lmfit.models fails
Asked Answered
S

0

1

This post is related to my other one where I was told to use lmfit to fix my problem.

I installed lmfit in anaconda Prompt with conda install --channel "conda-forge" lmfit which worked. But now when I try to use it in my Programm I get the following error:

ModuleNotFoundError: No module named 'lmfit.model'; 'lmfit' is not a package

Thats how the beginning of my Program looks like:

import numpy as np
import math
import scipy    #I dont need scipy, but I read, that lmfit needs scipy. It doesn't change anything when its there or not.
import matplotlib.pyplot as plt
from lmfit.model import GaussianModel

I also tried to import lmfit seperatly like:

import lmfit
from lmfit.model import GaussianModel

But it doesn't work either...

I tried everything from the lmfit website, but using the "git"-thing in Anaconda Prompt gives me an error (something like command doesn't exist)

I also found this post, and importing from lmfit import * works. Now I have the problem that I somehow need to import the GaussianModel, which I don't know how to do. If I just add from lmfit import GaussianModel I get the Error

ImportError: cannot import name 'GaussianModel'

I also read something about the order of importing packages and modules is important - is this what the problem is about? How can I fix this?

I work on windows with anaconda/spider.

Solidago answered 20/2, 2018 at 21:8 Comment(7)
It's plural: from lmfit.models import GaussianModel.Morceau
That, however, does not explain the error message you quote. Check that you're not running this in a directory where you have an lmfit directory or lmfit.py file, since in that case, Python will attempt to import that (which is likely not the installed package).Morceau
Just to be sure if I understand you correctly: I need to go to preferences - Current Working directory and need to choos the directory where lmfit (lmfit-0.9.7-py36_0.json) is (which would be Anaconda/ conda-meta for me)? Or did I missunderstand you?Solidago
Either pip install lmfit or conda install -c gsecars lmfit should work. After seeing one of those work, verify that you can do import lmfit from the python or ipython prompt in spyder or from a python session started in the "anaconda prompt". You do NOT need to be in the folder where lmfit got unpacked. In fact, you don't want a folder or file named lmfit in your working directory -- that will cause the confusion Evert is talking about.Lorylose
No, you want to avoid the directory where you downloaded lmfit, and where it's installed. I don't know how Spyder usually works with directories, since I'm used to the command line, but it seems likely there's a clash between filenames.Morceau
Here's a thing to try: only import lmfit, then print(lmfit.__file__) will tell you what file (with its full path) it is using. See if that looks like the correct file, or an incorrect file.Morceau
@MNewville @Evert Thank you very much, now it works! I did everything you told me, i guess that conda install -c gsecars lmfit made it work :) The path of my file is the following (for others which have the same problem): C:\Users\Carina\Anaconda3\lib\site-packages\lmfit-0.9.8-py3.6.egg\lmfit__init_.py_Solidago

© 2022 - 2024 — McMap. All rights reserved.