Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pathlib import not working
#1
This is my first attempted script so it is of course not working.

I am trying to open outlook, find an email, and download the attachment

The first line of the tut I am looking at says I need to import path from pathlib and win32com.client.
I get an indentation Error for line 1 the pathlib path import. It is supposed to be a core python module and not need installing like pywin32 so I do not know what it wants.

from pathlib import path
import win32com.client


output_dir =path.cwd() / "outbut"
output_dir.mkdir(parents=true, exist_ok=true)

outlook = win32com.client.dispatch("outlook.application").getnamespace("MAPI")

inbox =outlook.getfaultfolder(6)

messages = inbox.items

for message in messages:
    subject = Punch.report
    body = Message.body
    attachments = message.Attachments

for attachment in attachments:
    attachment.SaveAsFile(Target_folder / str(Attachment))

Attached Files

.py   main.py (Size: 494 bytes / Downloads: 142)
Reply
#2
You cannot import path from pathlib. It is Path with a capital P
from pathlib import Path
Reply
#3
Thank you.

What is the criteria for when something must be capitalized vs when it does not need to be?
Reply
#4
ok, now its giving me an error for the Win32com.client.
Module not found error.
It seems to be a location error, but the win32com folder is present in my file structure as well as the folder client. I'm not even sure what it is trying to import since win32com and client are both folders
Reply
#5
(May-29-2022, 01:32 PM)chriswrcg Wrote: What is the criteria for when something must be capitalized vs when it does not need to be?
If a module contains a thing named Path, it is named Path and not path. There is no way to guess if you don't know the module or don't read its documentation. An informal rule is that class names are capitalized and other values are not, but this is not true for many of the classes of the standard library such as int or list because the names existed long before the informal rule (pep 8) was invented.

chriswrcg Wrote:ok, now its giving me an error for the Win32com.client.

Again Win32com.client must not be capitalized here.
chriswrcg Wrote:Module not found error.
When posting errors, please post the entire error traceback that Python prints on the screen. Hiding error messages is not the best way to get them solved.
Reply
#6
(May-29-2022, 02:12 PM)chriswrcg Wrote: I'm not even sure what it is trying to import since win32com and client are both folders
You shall not name any folder with these names will,then will win32com look in wrong places.
Install pywin32 which win32com is part of and test that it work.
# Install
G:\div_code
λ pip install pywin32
Collecting pywin32
  Downloading pywin32-304-cp310-cp310-win_amd64.whl (12.1 MB)
     ---------------------------------------- 12.1/12.1 MB 4.9 MB/s eta 0:00:00
Installing collected packages: pywin32
Successfully installed pywin32-304

# Test
G:\div_code
λ python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32com.client
>>>
>>> exit()
Reply
#7
I have installed it. Both at the command prompt as well as directly from the authors website. It still will not work.
I tried creating a new project with a different file tree directing it (I thought) to the folder C:\Users\con_mcgheed\Documents\Python\venv\Lib\site-packages\win32com and it still will not "see" it.

from pathlib import Path  #core python module
import win32com.client  #pip install pywin32
  File "C:\Users\con_mcgheed\Documents\Python\venv\Lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ModuleNotFoundError: No module named 'win32api'
Reply
#8
When you use venv means that you use virtual environment(do you now how this work)?
Then most activate environment and install pywin32 there and it will use python.exe from environment and not from OS.
Reply
#9
where should I install it? I was following a youtube video. First attempt at Python.
Reply
#10
(May-29-2022, 06:07 PM)chriswrcg Wrote: where should I install it? I was following a youtube video. First attempt at Python.
Do you use PyCharm?
Look like you have chosen to create a new Python interpreter with virtual environment.
You either install to the OS version of Python or make virtual environment and install it there.
For basic setup look Python and pip installation under Windows and test that as shown there that python and pip work from cmd.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  deleting an empty Dir, using pathlib.Path tester_V 9 5,868 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,500 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,706 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,226 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  How to keep a loop containing a web import working in python executable? coder1384 3 2,890 Feb-22-2020, 06:49 PM
Last Post: snippsat
  import logging not working segerhult 3 13,237 Feb-07-2019, 05:31 PM
Last Post: buran
  CSV import is not working gehrenfeld 7 4,249 Dec-12-2018, 12:26 PM
Last Post: gehrenfeld
  pathlib hanging bluefrog 2 3,139 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,519 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  makin hardlinks with pathlib.Path Skaperen 2 5,241 Sep-06-2018, 07:53 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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