Python Forum

Full Version: Pathlib import not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
You cannot import path from pathlib. It is Path with a capital P
from pathlib import Path
Thank you.

What is the criteria for when something must be capitalized vs when it does not need to be?
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
(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.
(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()
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'
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.
where should I install it? I was following a youtube video. First attempt at Python.
(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.