New release: v0.6.1.

## Running own model yields ModuleNotFoundError

How do I use this algorithm? What does that parameter do?
frenzie92
Posts: 4
Joined: 14 Nov 2019, 16:07

### Running own model yields ModuleNotFoundError

Hello all. Sorry if this is not the correct place to ask this.

So I've written my own model (the details of the model are not important for this question), and the relevant code is

Code: Select all

import numpy as np

from .lattice import Lattice, Site, Chain
from ..networks.site import FermionSite
from .model import MultiCouplingModel, CouplingMPOModel
from ..tools.params import get_parameter
from ..tools.misc import any_nonzero

__all__ = ['FQHChain']

class FQHChain(CouplingMPOModel, MultiCouplingModel):
"""

def __init__(self, model_params):
CouplingMPOModel.__init__(self, model_params)

def init_sites(self, model_params):
site = FermionSite(conserve='N')
return site

def init_lattice(self, model_params):
site = self.init_sites(model_params)
L = get_parameter(model_params, 'L', 2, self.name) # default values of parameters are currently arbitrary.
bc_MPS = get_parameter(model_params, 'bc_MPS', 'infinite', self.name)
bc = 'periodic' if bc_MPS == 'infinite' else 'open'
lat = Chain(L, site, bc=bc, bc_MPS=bc_MPS)
return lat

def init_terms(self, model_params):
V10 = get_parameter(model_params, 'V10', 1., name, asarray=True)  # default values of parameters are currently arbitrary.
V20 = get_parameter(model_params, 'V20', 0.2, name, True)
V21 = get_parameter(model_params, 'V21', 0.5, name, True)
self.add_coupling(V10, 0, 'N', 0, 'N', 1)
self.add_coupling(V20, 0, 'N', 0, 'N', 2)
self.add_multi_coupling(-V21, 0, 'Cd', [(0, 'C', 1), (0, 'C', 2), (0, 'Cd', 3)] )
self.add_multi_coupling(-V21, 0, 'C', [(0, 'Cd', -1), (0, 'Cd', -2), (0, 'C', -3)] )  # h.c. of previous term
# done

The script of this code is called 'fqh_chain.py', and I've saved it in the directory

Code: Select all

/Users/username/TeNPy/tenpy/models

where all the other models are as well. I try calling it in another script (which is on my desktop) by running

Code: Select all

from tenpy.models.fqh_chain import FQHChain

and I get the error

Code: Select all

Traceback (most recent call last):
File "fqh_test.py", line 1, in <module>
from tenpy.models.fqh_chain import FQHChain
ModuleNotFoundError: No module named 'tenpy.models.fqh_chain'

Yet, when I call one of the pre-existing models, e.g.

Code: Select all

from tenpy.models.toric_code import ToricCode

I get no errors!

Does anybody know why this happens?

I am new to python 3, this is the first time I'm using it.

Johannes
Posts: 176
Joined: 21 Jul 2018, 12:52
Location: UC Berkeley

### Re: Running own model yields ModuleNotFoundError

Welcome!

I think the problem depends on how you installed tenpy. Did you pip install physics-tenpy, or did you download the code and use inline]python setup.py install[/inline] from within the TeNPy folder, or did you simply set export PYTHONPATH=/Users/username/TeNPy"? In the latter case, it should work. In the former cases, pip/setuptools might have created a copy of the files somewhere, for example it could be a folder in /usr/lib/python3.6/site-packages/physics_tenpy-0.4.1-py3.6-linux-x86_64.egg/.
To find out where exactly a file you imported is located, you can for example

Code: Select all

import tenpy
print(tenpy.__file__)

.
Try pip uninstall physics-tenpy to remove that copy.

If you want to modify/ add files to the tenpy version in your home directory, you should
 pip install --editable . from within the TeNPy folder, which will tell python that you want to import files from this folder in your home directory.

In most cases, however, I would recommend that you keep a clean, unmodified version of tenpy installed, and add the model simply as a separate file in the folder where you set up your scripts to call tenpy. That will lead to less confusion if you have multiple versions of the model file, where you try out new things - you simply have separate copies in separate "project" folders.

In your case, you should move the fqh_chain.py to for example a folder /User/username/projects/my_first_attempt/,
and replace the relative imports like from .lattice import Lattice, Site, Chain with from tenpy.models.lattice import Lattice, Site, Chain,
from ..networks.site import FermionSite with from tenpy.networks.site import FermionSite, and so on.
Then you can create another file run_my_simulation.py, which could look like this:

Code: Select all

from fqh_chain import FQHChain
from tenpy.algorithms import dmrg
from tenpy.networks.mps import MPS

M = FQHChain({})  # adjust parameters...
psi = MPS.from_product_state(M.lat.mps_sites(), ["empty"]*M.lat.N_sites)
E, psi = dmrg.run(M, psi, {})
# ...

PS: See https://docs.python.org/3.7/tutorial/modules.html for an introduction to modules

frenzie92
Posts: 4
Joined: 14 Nov 2019, 16:07

### Re: Running own model yields ModuleNotFoundError

Thanks a lot! That was very helpful! Now I have a different question, but it's on a different topic so I'll just ask it anew since it's unrelated to this issue!

Thanks again.