10

I've just installed Python for the first time and I'm trying to reference the win32com module however, whenever I try to import it I get the message "no module name win32com".

Any ideas?

CBusBus
  • 2,321
  • 1
  • 18
  • 26

6 Answers6

12

the below plus add pywin32 in PyCharm setting work for me

python -m pip install pywin32
9

As it is not built into Python, you will need to install it.

pip install pywin

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • I've managed to install pywin just now however, import win32com (or pywin) returns the same error message. – CBusBus Feb 21 '16 at 12:11
  • @CBusBus Can you check the output of print(sys.path) and pip install pywin? Maybe the package is installed into a wrong place. – JxCode Dec 19 '19 at 20:37
5

Since win32com is a Windows-specific package, this answer will be geared towards Windows users.

Option 1: Install locally with pipenv (recommended)

You can use a package manager like pipenv to manage your dependencies.

  1. Ensure you have pipenv installed (pip install pipenv).
  2. In your project directory, run pipenv install pypiwin32 to install the package.
  3. Now you can run your code using commands like the following pipenv run main.py

Example main.py code:

import win32com

print(win32com)

Option 2: Install locally with venv (recommended)

If pipenv isn't your thing, you can use the built-in virtual environments.

  1. From your project directory, run python -m venv venv to setup you virtual environment.
  2. Run venv\Scripts\activate.bat from your project directory whenever you want to use this virtual environment (you will see (venv) added to your shell prompt to know it's activated).
  3. Run pip install pypiwin32 from your active virtual environment to install the package.
  4. You can run your code like python main.py so long as the virtual environment is active.

Option 3: Install globally (typically not recommended)

This is not typically recommended, but included anyway.

  1. Using pip install pypiwin32 you can install the package globally.
  2. Then you can run your code with just python main.py.
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
1

When working with python projects its always a good idea to create a so called virtual environment, this way your modules will be more organized and reduces the import errors.

for example lets assume that you have a script.py which imports multiple modules including pypiwin32.

here are the steps to solve your problem:

  • 1. depending on you operating system you need to download and install virtualenv package, in debian its as simple as sudo apt install virtualenv .
  • 2. after installing 'virtualenv' package go to your project/script folder and create a virtualenv folder with virtualenv venv it creates a folder named venv in that directory.
  • 3. activate your virtualenv source /path/to/venv/bin/activate if your already in the directory where venv exists just issue source venv/bin/activate
  • 4. after activating your venv install you project dependencies pip install pypiwin32 or pip install pywin
  • 5. run your script, it wont throw that error again :)
  • arez
    • 61
    • 5
    1

    This will work as well

    python -m pip install pywin32

    0

    You should try using pip this way:

    pip install pypiwin32
    

    It is pypiwin32 which should work.

    U13-Forward
    • 69,221
    • 14
    • 89
    • 114