Python Forum

Full Version: My import not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Everybody

I am trying to learn python and I created my first script with a package that contains an imported module but it does not work!
Both scripts work independently. 

I put the package in the site-packages of the virtualenv and no import.
I tried many things but nothing worked and I understood how the import works!

I do not understand why it does not work! Can you help me please ?

Thanks a lot


My OS: Debian 8 KDE
IDE: PyCharm also Vim + terminal
Virtualenv: Python 3.5
My Working directory: PycharmProject
My sys.path with Pycharm:
>>> import sys
>>> sys.path
['/home/user/bin/PYTHON/pycharm-community-2016.2.3/helpers/pydev', '/home/user/bin/PYTHON/PycharmProject', '/home/user/bin/PYTHON/pycharm-community-2016.2.3/helpers/pydev', '/home/user/bin/PYTHON/virtualenv/py35/lib/python35.zip', '/home/user/bin/PYTHON/virtualenv/py35/lib/python3.5', '/home/user/bin/PYTHON/virtualenv/py35/lib/python3.5/plat-linux', '/home/user/bin/PYTHON/virtualenv/py35/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5', '/usr/local/lib/python3.5/plat-linux', '/home/user/bin/PYTHON/virtualenv/py35/lib/python3.5/site-packages', '/home/user/bin/PYTHON/PycharmProject']
>>> 
~/bin/PYTHON/PycharmProject$ tree
├── courier
│   ├── envoi.py
│   └── __init__.py
└── DiskSpace.py
The Two littles scripts:

The main script:
#!/usr/local/bin/python3.5
# -*- coding: utf-8 -*-

import subprocess
#import sys
#sys.path.append("/home/user/bin/PYTHON/PycharmProject")
#import courier.envoi
from courier.envoi import MonCourier


class DiskSpace:

    def espace_disk(self):
        p = subprocess.Popen("df -h", stdout=subprocess.PIPE, shell=True)
        (output, err) = p.communicate()
        tableau = output.split()

        for index, ligne in enumerate(tableau):
            if tableau[index].decode('utf8') == "/home":
                val_index = int(index)
                val_index_dispo = (val_index - 2)
                val_giga_dispo = tableau[val_index_dispo].decode('utf8')
                valeur = int(val_giga_dispo.replace("G", " "))

                if valeur <= 5:
                    print("ATTENTION ! ALERTE ! {}G de disponible sur le disk !".format(valeur))
                    envoi_mon_courier("ALERTE ! {}G de disponible sur le disk !".format(valeur))
                else:
                    print("Espace disk disponible est de {}G .".format(valeur))
                    envoi_mon_courier("Espace disque est de {}G .".format(valeur))

if __name__ == '__main__':
    disk_server = DiskSpace()
    disk_server.espace_disk()
The module for import:
#!/usr/local/bin/python3.5
# -*- coding: utf-8 -*-

import smtplib
from smtplib import SMTPException
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


class MonCourier:

    def envoi_mon_courier(self, msg_info):
        msg = MIMEMultipart()
        msg['From'] = '[email protected]'
        msg['To'] = '[email protected]'
        msg['Subject'] = 'Espace Disque Server!'
        message = msg_info
        msg.attach(MIMEText(message))
        try:
            # Send the message via our own SMTP server.
            mailserver = smtplib.SMTP('smtp.domain.com', 587)
            mailserver.ehlo()
            mailserver.starttls()
            mailserver.ehlo()
            mailserver.login('[email protected]', 'admin')
            mailserver.sendmail('[email protected]', '[email protected]', msg.as_string())
            print("Successfully sent email")
            mailserver.quit()
        except SMTPException:
            print("Error: unable to send email")

#if __name__ == '__main__':
#    s = MonCourier()
#    s.envoi_mon_courier("hello")
You're missing the most important part. Please post your error trace.

Thanks
Thanks ! Sorry ...

/home/user/bin/PYTHON/virtualenv/py35/bin/python /home/user/bin/PYTHON/PycharmProject/DiskSpace.py
Traceback (most recent call last):
  File "/home/user/bin/PYTHON/PycharmProject/DiskSpace.py", line 34, in <module>
    disk_server.espace_disk()
  File "/home/user/bin/PYTHON/PycharmProject/DiskSpace.py", line 27, in espace_disk
    envoi_mon_courier("ALERTE ! {}G de disponible sur le disk !".format(valeur))
NameError: name 'envoi_mon_courier' is not defined
ATTENTION ! ALERTE ! 46G de disponible sur le disk !

Process finished with exit code 1
envoi_mon_courier seems to be a method of class MonCourier...
So you should call it with
MonCourier.envoi_mon_courier("ALERTE ! {}G de disponible sur le disk !".format(valeur))
Hello,

You need to start reading the error messages, they will always tell you what's wrong (with blatant errors).
The trace shows the last few commands that were executed, and the final one shows the line where the system crashed.


Quote:
NameError: name 'envoi_mon_courier' is not defined

This certainly is at least issue number 1.

it requires the name that you assigned to the import:
MonCourier.envoi_mon_courier
Larz60+