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?
the below plus add pywin32 in PyCharm setting work for me
python -m pip install pywin32
As it is not built into Python, you will need to install it.
pip install pywin
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.
Ensure you have pipenv installed (pip install pipenv).
In your project directory, run pipenv install pypiwin32 to install the package.
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.
From your project directory, run python -m venv venv to setup you virtual environment.
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).
Run pip install pypiwin32 from your active virtual environment to install the package.
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.
Using pip install pypiwin32 you can install the package globally.
Then you can run your code with just python main.py.
This will work as well
python -m pip install pywin32
You should try using pip this way:
pip install pypiwin32
It is pypiwin32 which should work.
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 :)
Related
I have a Flask app that with and using venv for my virtual environment. For some reason, which pip has suddenly stopped installing packages in venv/lib/python3.9/site-packages, but to a completely different repository on my system. How do I redirect pip to install packages to the correct path in venv?
instead of using pip as a stand alone command in terminal, i suggest you to use python -m pip instead, here python will be assoicated with the project (in case of Virtual environment) or whole system level (in case docker single app ). This will keep track and tell the system to run all package/module related to python interpreter assoicated with the project only
You can use the -t flag provided by the pip install command.
pip install <package_name> -t <full_location_path>
I installed VMWare on window and using a virtualenv for python project.
I activated the project project1 and pip3 install xxxxx modules I need.
But some of them had an import error and when I deactivated the virtualenv and pip3 install xxxx on the root, the import error is resolved.
Are we supposed to pip install on the root, even though we activate the virtualenv? Thanks.
# A virtualenv's python pip call:
$ project1/bin/python -m pip install xxxxx
Where:
project1: Is the env folder inside your root folder that contain all
your python modules included python it self.
-m is a python flag stand for: "run library module as a script" .
pip: Is the package manager.
install: is the pip command to install the desired module.
xxxxx: its the desired python module (no, its not about film genres)
P.S: In this way you ensure to be using the python version you installed on your virtualenv unequivocally
I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages
I am new to Python. Today I installed flask in C:Users\myName\FolderA using below commands and it worked fine. But when i try to create a structure C:Users\myName\FolderA\FolderB and create app.py in it, my VSCode says "ModuleNotFoundError: No module named 'flask'". Does this mean i need to install flask in Folder B too? Is there a way to install flask globally and make all folders under 'FolderA' access the libs?
Commands used to install Flask
C:Users\myName\FolderA\py -m venv env
C:Users\myName\FolderA\env\Scripts\activate
(env)c:Users\myName\FolderA\pip install flask
You are using a virtualenv. Everything installed inside a virtualenv resides inside that virtualenv (which is nice in fact: all your dependencies for that project only!).
If you want to install packages globally, run
pip install flask
without having run the activate script first (that activates your virtualenv).
PIP should install packages globally. You shouldn't have to reinstall flask everytime. Seems like something is wrong with your PIP. How did you install Python?
You could try updating PIP, which could fix it. Try running
python -m pip install –upgrade pip
in cmd.
Edit: This could also be an issue with Visual Studio Code. Try running the python code through CMD.
I have a MacBook Pro that came pre-installed with python2.7. I later installed python3 and ipython notebook. I installed pip too to install packages, and am able to install packages and run program from python3. However, for another project I need to run code in python2.7, and I am not sure how to install it in python2.7 folder.
I tried using pip for installing packages to 2.7, but it kept giving error saying package already exists. When I check for version of python using --version, I see 2 pythons installed. However, when I check for pip and pip3, both seem to be in th same folder.
Any tips on how to install packages in python2.7, without making any changes to 3.3? I am using python3 and ipython notebooks for another project.
viveks-mbp:~ vivekyadav$ which pip
/Library/Frameworks/Python.framework/Versions/3.3/bin/pip
viveks-mbp:~ vivekyadav$ which pip3
/Library/Frameworks/Python.framework/Versions/3.3/bin/pip3
viveks-mbp:~ vivekyadav$ which python
/usr/bin/python
viveks-mbp:~ vivekyadav$ which python3
/Library/Frameworks/Python.framework/Versions/3.3/bin/python3
You can use the virtualenv to create a kind of sandbox.
$ virtualenv <work-directory>
$ source <work-directory>/bin/activate
The last command initiate your virtual environment, totally isolated from the system. So every pip command will install the package inside this directory.
But you have to run your application inside the virtual environment too.