1

I am trying to run this program:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from io import open

from multiprocessing import Pool
import buildingspy.simulate.Simulator as si


# Function to set common parameters and to run the simulation
def simulateCase(s):
''' Set common parameters and run a simulation.

:param s: A simulator object.

'''
s.setStopTime(86400)
# Kill the process if it does not finish in 1 minute
s.setTimeOut(60)
s.showProgressBar(False)
s.printModelAndTime()
s.simulate()


def main():
''' Main method that configures and runs all simulations
'''
import shutil
# Build list of cases to run
li = []
# First model
model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
s = si.Simulator(model, 'dymola', 'case1')
s.addParameters({'con.eOn': 0.1})
li.append(s)
# second model
s = si.Simulator(model, 'dymola', 'case2')
s.addParameters({'con.eOn': 1})
li.append(s)

# Run all cases in parallel
po = Pool()
po.map(simulateCase, li)

# Clean up
shutil.rmtree('case1')
shutil.rmtree('case2')

# Main function
if __name__ == '__main__':
main()

and I keep getting this error:

File "C:/Users/Toshiba/.spyder-py3/temp.py", line 11, in <module>
    import buildingspy.simulate.Simulator as si

ModuleNotFoundError: No module named 'buildingspy'

I already installed the package using pip more than one time and nothing changes.

What am I missing?

This is the source of this code.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50

1 Answers1

0

That error might be due to having multiple python installations on your computer:
https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel

Please add the following lines somewhere to your script (or to a new script) and run it once from Spyder, and once from the console and compare the output:

import sys
print("python: {}".format(sys.version))
# also add the following if running from python 3
from shutil import which
print(which("python"))

Buildingspy has to be installed using pip, I would recommend to install it using the command:

python -m pip install -U https://github.com/lbl-srg/BuildingsPy/archive/master.zip

Anaconda adds an Anaconda prompt to the Start menu, use that to make sure the path to python.exe is correct.

Once BuildingsPy is installed correctly, you will run into the problem that on Windows multiprocessing will not work from Spyder (or, from IPython/Jupyter), please also read this issue:
https://github.com/lbl-srg/BuildingsPy/issues/179
You will have to run your script from the command line.

matth
  • 2,568
  • 2
  • 21
  • 41
  • I ran your 2 line code and got it using the CMD calling Python: python: 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] Then using anaconda (spyder): python: 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:28:48) [MSC v.1900 64 bit (AMD64)] I interpreted as I have only one installation, right? – Ricardo de Castro Jan 09 '18 at 11:06
  • then I went to the command prompt and tried to run the script (after installing the package as you said to) and I got few lines with – Ricardo de Castro Jan 09 '18 at 11:18
  • Traceback (most recent call last): File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\process.py", line 258, in _bootstrap self.run() File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\process.py", line 93, in run self._target(*self._args, **self._kwargs) File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\pool.py", line 108, in worker task = get() File – Ricardo de Castro Jan 09 '18 at 11:18
  • "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\queues.py", line 337, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'simulateCase' on Process SpawnPoolWorker-3: Traceback (most recent call last): File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\process.py", line 258, in _bootstrap self.run() File – Ricardo de Castro Jan 09 '18 at 11:18
  • "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\process.py", line 93, in run self._target(*self._args, **self._kwargs) File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\pool.py", line 108, in worker task = get() File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\queues.py", line 337, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'simulateCase' on **bold** Am I still missing a step? – Ricardo de Castro Jan 09 '18 at 11:19
  • I edited the script a tiny bit, it now also uses shutil which. But I also believe you have only one installation and the issue is a different one. – matth Jan 09 '18 at 11:23
  • The error message seems to be a differen tone now, is that right? So buildingspy is now found and instead you are running into multiprocessing problems? Do other examples work as expected? – matth Jan 09 '18 at 11:29
  • python: 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] C:\Users\Toshiba\Anaconda3\python.EXE again, in both cases – Ricardo de Castro Jan 09 '18 at 12:07
  • Yes, now the package is found and I am getting this errors I copied and paste. Those errors are given when I run the code from the Command prompt when I tried to run using spyder It keeps running forever. I checked my task manager to see the CPU usage and it was at 99% and most part was due to the "avast behaviour check" ... is it related to the problem you mentioned on GitHub ? – Ricardo de Castro Jan 09 '18 at 12:10
  • now, running the second example with anaconda I get this – Ricardo de Castro Jan 09 '18 at 14:52
  • File "C:\Users\Toshiba\Anaconda3\lib\multiprocessing\pool.py", line 644, in get raise self._value AttributeError: 'NoneType' object has no attribute 'write' – Ricardo de Castro Jan 09 '18 at 14:52
  • That sounds like the linked issue. Did you try the steps mentioned there, i.e. does it work when you run the script from the terminal instead of from Spyder? Does it work on Linux? https://github.com/lbl-srg/BuildingsPy/issues/179 – matth Jan 10 '18 at 08:06
  • *** Error: Terminating simulation in C:\Users\Toshiba\AppData\Local\Temp\tmp-simulator-Toshiba-jc8_w4r0\Buildings. *** Error: Killing simulation in C:\Users\Toshiba\AppData\Local\Temp\tmp-simulator-Toshiba-jc8_w4r0\Buildings. *** Error: Killed process as it computed longer than 240 seconds. *** Error: Simulation failed in 'C:\Users\Toshiba\AppData\Local\Temp\tmp-simulator-Toshiba-jc8_w4r0\Buildings' Exception: File C:\Users\Toshiba\AppData\Local\Temp\tmp-simulator-Toshiba-jc8_w4r0\Buildings\simulator.log does not exist. You need to delete the directory manually. – Ricardo de Castro Jan 10 '18 at 11:42
  • That is an error message from Buildingspy indeed, but it is not very clear. I am guessing that the simulation does not finish for some reason, and therefore the process gets killed. To find out the reason, you can can turn on the Dymola GUI. One possible reason is that the model you are trying to simulate does not get found. Are all pathes correct, including the path to the Buildings library (the script is trying to simulate a model from that lib)? – matth Jan 10 '18 at 20:03