0

I have the following file structure:

command.py
simulations
  basis
    basis.py
  hamiltonian
    hamiltonian.py

where the names without extensions are folders.

  • command.py is importing basis.py and hamiltonian.py like:

from basis.basis import Basis

from hamiltonian.hamiltonian import Hamiltonian

where Basis and Hamiltonian are two classes.

I can run command.py fine, all the imports are Okay.

  • Now, I want to work with hamiltonian.py alone, but it needs to import basis.py.

  • In order for command.py to work fine, the import in hamiltonian.py has to be from basis.basis import Basis

  • In order for hamiltonian.py to run on its own, the import needs to be

    os.chdir('..')
    from basis.basis import Basis
    however this makes command.py not work anymore.

--

1) Can I somehow run the os.chdir('..') only if hamiltonian.py is run on its own? Like with if name == 'main'?

2) Is there a more elegant solution to this?

SuperCiocia
  • 1,823
  • 6
  • 23
  • 40
  • 1
    you need \_\_init__.py files in those directories that will act as modules (simulations I supose) Then in commands you have.from simulations.whatever.whatever import whatever And in hamiltonians from simulations.whatever.whatever import whatever – E.Serra Aug 21 '18 at 15:05
  • yes I have an init. The issue is that hamiltonian is used also by command as well – SuperCiocia Aug 21 '18 at 15:48
  • by the looks of it...this looks like a bad design for classes in first place! – NoobEditor Aug 21 '18 at 17:48
  • @NoobEditor then could please tell me what a better method could be? basis.py and hamiltonian.py are producing npy arrays and pdfs which I am saving in the same folder. Which is why I wanted to put them in separate folders. So with this folder structure, what would be a better class design in your opinion? – SuperCiocia Aug 21 '18 at 22:18
  • Google something about repo structure and init files packages and modules. Not trying to be rude, but you Will thank me in a couple of months. – E.Serra Aug 23 '18 at 11:28

1 Answers1

1

1) You can, but its not a good idea. It is better to avoid using os.chdir.

2) The fact that you need os.chdir suggests that you are trying to run it locally like:

python ./simulations/hamiltonian/hamiltonian.py

If this is the case, use:

PYTHONPATH=. python ./simulations/hamiltonian/hamiltonian.py

You will not run into this problem if you properly install the python package and your package has the proper __init__.py files as suggested by @E.Serra.

Ghasem Naddaf
  • 862
  • 5
  • 15
  • Sorry, what do you mean by properly install? THey are both scripts that I have written. My init is empty, it's just there so that the folder is seen as a python repository. What should the init have to be "proper"? – SuperCiocia Aug 21 '18 at 21:48
  • See https://stackoverflow.com/questions/42494229/how-to-pip-install-a-local-python-package for installing local python package. Empty `__init__.py` files are fine; by "proper" I meant that you have them in all directories that are needed (i.e. in `simulations` as well as in `basis` and in `hamiltonian`) – Ghasem Naddaf Aug 21 '18 at 22:15
  • I definitely have all the inits, because I can find all the imports, if I just use chdir. Why should I not use os.chdir? – SuperCiocia Aug 21 '18 at 22:19
  • What's the difference between importing my script and installing a local pacakge? – SuperCiocia Aug 21 '18 at 22:20
  • Also from what I can see PYTHONPATH is only used when running stuff from the terminal? I am indeed trying to run it locally, but always within spyder – SuperCiocia Aug 21 '18 at 22:23
  • Installing the package basically puts it to a known path. see e.g. https://github.com/flatironinstitute/CaImAn/wiki/Setting-up-Python-and-getting-to-know-Spyder#the-pythonpath-variable . Also you can specify PYTHONPATH before running `spyder` please consult the same link. – Ghasem Naddaf Aug 21 '18 at 23:16
  • OKay yes this worked very nicely with the PYTHONPATH! Thanks. – SuperCiocia Aug 22 '18 at 14:41
  • This is a one time sesión answer also do not overwrite pythonpath, append to it instead PYTHONPATH=PYTHONPATH:ADDITIONAL PYTHON PATH. This Will do the trick but it is not a good solución. – E.Serra Aug 23 '18 at 11:30