Question

I have created a python (I m using 2.4) script to automatically create a virtual host in httpd.conf. But when I run it it gives the following error:

Traceback (most recent call last):
  File "ApaPy2.py", line 2, in ?
    from io import open
ImportError: No module named io

This is my script

import os
from io import open
project = raw_input(u'Enter the name of project ')
domain = raw_input (u'Enter the domain ')
docroot = raw_input(u'Enter root folder ')

virtualhost=u"""
<VirtualHost *:80>
    ServerAdmin abhishek.verma@laitkor.com
    DocumentRoot /""" +docroot+ u"""/""" +project+ u"""
    ServerName """ +project+ u""".""" +domain+ u""".com
    ErrorLog logs/""" +project+ u""".com-error_log
    CustomLog logs/""" +project+ u""".com-access_log common
</VirtualHost>"""

f = open(u'/etc/httpd/conf/httpd.conf', u'a')
f.write(virtualhost)
f.close()
Était-ce utile?

La solution 2

The io module was introduced in Python 2.6, so it doesn't exist in 2.4. From the documentation:

New in version 2.6.

The open keyword should work fine for what you're doing here.

Autres conseils

The io module doesn't exist in 2.4 (and you don't need it to use open in this case). I would also simplify your code to use string formatting using % instead:

project = raw_input(u'Enter the name of project ')
domain = raw_input (u'Enter the domain ')
docroot = raw_input(u'Enter root folder ')

virtualhost=u"""
<VirtualHost *:80>
    ServerAdmin abhishek.verma@laitkor.com
    DocumentRoot /%(docroot)s/%(project)s
    ServerName %(project)s.%(domain)s.com
    ErrorLog logs/%(project)s.com-error_log
    CustomLog logs/%(project)s.com-access_log common
</VirtualHost>"""

f = open(u'/etc/httpd/conf/httpd.conf', u'a')
f.write(virtualhost % dict(project=project, docroot=docroot, domain=domain)
f.close()

I've never used python 2.4, but the documentation says the io module has been added in the 2.6 version, so you can't import it in 2.4.

I'd assume open was already a built in function in 2.4, though, so simply removing the from io import open line should be enough.

The io module didn't exist in Python 2.4. Your usage of open is simple, so you can omit that line and the open statement will still work correctly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top