There is a file with a "configuration" from which variables are taken.

File - __main__.py

import sys import getopt import os import picamera from raspm import raspm camera = None camlock = Lock() USAGE = ''' Usage %s [OPTIONS] COMMAND COMMANDS: run -- Start RPM as Raspberry Pi Manager (default) http-server -- Start only http server CONFIGURATION: - Configuration specified on the command line - /etc/rpm.conf - ./etc/rpm.conf ''' def usage(retval = 0): print(USAGE % sys.argv[0]) sys.exit(retval) if __name__ == "__main__": cfg = '/etc/rpm.conf' if os.path.isfile('/etc/rpm.conf') else './etc/rpm.conf' try: app.run(host='0.0.0.0', port='8080') opts, args = getopt.getopt(sys.argv[1:], 'hc:' ['help', 'config=']) finally: #After work is finished shut off the camera with camlock: if camera: camera.close() 

File - raspm.py

 .... def update_configuration(cfgfile): from configobj import ConfigObj from raspm.config import cfgspec from validate import Validator cfg = ConfigObj(cfgfile, configspec=cfgspec) validator = Validator() cfg.validate(validator) globals()['CONFIG'] = cfg return cfg # Set up configuration CONFIG = update_configuration('/etc/rpm.conf') ... 

Further config file config.py

 # -*- coding: utf-8 -*- ''' Default configuration. ''' __CFG = ''' ...... ''' cfgspec = __CFG.split('\n') 

And the last rpm.conf

When starting __main__.py I get the following:

 Traceback (most recent call last): File "__main__.py", line 19, in <module> from raspm import raspm File "/home/pi/python-RPi-camera-controll/raspm/raspm.py", line 73, in <module> CONFIG = update_configuration('./etc/rpm.conf') File "/home/pi/python-RPi-camera-controll/raspm/raspm.py", line 64, in update_configuration from rpm.config import cfgspec ImportError: No module named rpm.config 

files in folders:

 python/ - etc/ - - rpm.conf - raspm/ - - __main__.py - - config.py - - raspm.py 

I do not understand the problem

  • Obviously, the fact that there is no rpm.config module :) And where to get it from and what the rpm module is all about, and Google and I don't know - andreymal
  • @andreymal but for this reason that in my code, rpm.config should be taken from the folder - Insider
  • I suspect you need complete information about the folders and all the files in them - andreymal
  • one
    But we must not β€œstrive for the same”, but to understand the essence of how everything works and why it works. I will get to the computer - I will tell in the chat, if I do not get ahead. But still there are no text files imported :) - andreymal
  • one
    Here is a good example of bad code. All imports must be explicitly placed at the top of the scenarios. Themselves do crutches themselves, and then they wonder where the Lock () came from and why ImportError? - Xyanight

1 answer 1

 File "/home/pi/python-RPi-camera-controll/raspm/raspm.py", line 64, in update_configuration from rpm.config import cfgspec ImportError: No module named rpm.config 

The file raspm.py (64 line) should be:

 from raspm.config import cfgspec 

In the folder raspm/ put (empty) file __init__.py


 . β”œβ”€β”€ f1 β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ pf1.py β”œβ”€β”€ f2 β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ pf2.py └── main.py 

main.py:

 from f1.pf1 import fun1 fun1() 

pf1.py:

 from f2.pf2 import fun2 def fun1(): print "I am fun1" fun2() 

pf2.py:

 def fun2(): print "I am fun2" 

perform:

 $ python main.py I am fun1 I am fun2 

This is how import from subfolders in Python works. Well, I agree, all imports are at the beginning of the file.

  • Import from subfolders in Python works without init .py. It is enough to specify the path to the directory in sys.path. - Xyanight
  • @Xyanight, hmmm ... All the subfolders of the current project in sys.path write? You can, but why? - andy.37
  • one
    @Xyanight then the imported file will not be a submodule of the module, but the module itself, which is not always acceptable - andreymal