Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My import not working
#1
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")
Reply
#2
You're missing the most important part. Please post your error trace.

Thanks
Reply
#3
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
Reply
#4
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))
Reply
#5
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+
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pathlib import not working chriswrcg 9 3,734 May-29-2022, 07:37 PM
Last Post: snippsat
  How to keep a loop containing a web import working in python executable? coder1384 3 2,888 Feb-22-2020, 06:49 PM
Last Post: snippsat
  import logging not working segerhult 3 13,229 Feb-07-2019, 05:31 PM
Last Post: buran
  CSV import is not working gehrenfeld 7 4,245 Dec-12-2018, 12:26 PM
Last Post: gehrenfeld
  import keyboard module doesn't get found - working on laptop but no on raspberry pi.. HANSJORG2 1 8,175 Mar-16-2018, 02:48 PM
Last Post: sparkz_alot
  import not working properly mepyyeti 5 4,379 Jan-18-2018, 07:08 AM
Last Post: metulburr
  import commands modules not working in python 3.6.0 bmohanraj91 2 17,651 May-01-2017, 10:59 AM
Last Post: bmohanraj91
  import antigravity not working Skaperen 8 7,463 Feb-19-2017, 05:49 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020