CBusBus
CBusBus

Reputation: 2357

No module named win32com

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?

Upvotes: 12

Views: 47087

Answers (6)

Yibingqing Jiang
Yibingqing Jiang

Reputation: 151

the below plus add pywin32 in PyCharm setting work for me

python -m pip install pywin32

Upvotes: 15

Rahul Kumeriya
Rahul Kumeriya

Reputation: 204

This will work as well

python -m pip install pywin32

Upvotes: 2

Alexander O'Mara
Alexander O'Mara

Reputation: 60577

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.

Upvotes: 6

arez
arez

Reputation: 71

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 :)
  • Upvotes: 2

    U13-Forward
    U13-Forward

    Reputation: 71610

    You should try using pip this way:

    pip install pypiwin32
    

    It is pypiwin32 which should work.

    Upvotes: 1

    DeepSpace
    DeepSpace

    Reputation: 81684

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

    pip install pywin

    Upvotes: 9

    Related Questions