Actually, I have the same problem as him:
No module named win32com
I have installed pywin32 but MobaXterm tells me "No module named win32com".
However, I am able to run my program using IDLE with no error.
What's the problem?
Code:
import win32com.client
import sys, os
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut('C:/Users/Seaky/Desktop/CS 160.lnk')
os.chdir(shortcut.Targetpath)
What I am doing is that I am trying to do "cdlnk path" in the terminal using python code, where the path is a shortcut folder like the path above instead of a real path.
I used different code for running in the terminal and the IDLE but I only changed the path string from sys.argv[1] to the current one, which should not affect the result.
I have found out. The compiler of python installed in the MobaXterm doesn't have the pywin32 module.
Related
ScreenGrab of error when trying to import db I created
You either need to install Flask from pip, or something similar, or Python is not able to find the location of Flask if it is, in fact, installed.
At your Python prompt, try this to see where it is looking for libraries:
>> import sys
>> print (sys.path)
Window10 and Python3.7.4
The lib I am trying to use is 'colorama'.
When i run the python script file (x.py) it cannot find the module in the line which is "import colorama", but when i open a cmd and enter python env then type 'import colorama', it works. Does any one know what's the reason?
'''
>>>D:\Scratch\commands>mypythonscript.py
>>>Traceback (most recent call last):
>>> File "D:\Scratch\commands\mypythonscript.py", line 12, in <module>
>>> from colorama import init, Fore, Back, Style
>>>ModuleNotFoundError: No module named 'colorama'
'''
I don't know why this issue arise but maybe I installed VS2019. Before this, it worked fine for me.
Try to find which python your are working with using SYS module with its VERSION method in this way:
import sys
print(sys.version)
And then verify if you are using the same python version as you are using on cmd.
Another problem could be the path of your module, try to locate it with the "magical method" of your module installed, for example in this case:
import colorama
print(colorama.__file__)
And then verify if colorama is in this path: ".../Python37/lib/site-packages/colorama"
if not, download COLORAMA from here:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
unzip it with 7ZIP
and all unzip files copy to : ".../Python37/lib/site-packages"
don't worry for the module version, colorama is for all python versions.
NOTE: this only works for windows version.
I am very new to Python.
I am in command line and I typed the following:
python -m pip install -U pip
Python then starts working inside of command line.
When I type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
'Hello World!'
is printed.
When I go to the python shell and type
import pyperclip
pyperclip.copy('testing')
pyperclip.paste()
I get an error message:
ModuleNotFoundError: No module named 'pyperclip'
How can I figure out where command line is looking when I type import pyperclip and where it is looking in the python shell to compare the two?
Edit #1:
import sys
sys.path
Seems to be important. I am guessing that when a module is imported, python checks one of those locations.
pyperclip-1.5.7 is now located in one of the paths specified., the "Lib" directory.
pyperclip-1.5.7 looks like this inside of the folder:
All of the other modules are located as ".py" files just outside of the pyperclip-1.5.7, maybe I need to move some stuff there. I wonder how I can tell python to check inside of this directory...
Edit #2:
I went to this location: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
On the page, the author writes:
So Python will find any packages that have been installed to those
locations.
Let's see if I can add something to the sys.path location that points to the specific package that is inside the pyperclip-1.5.7 directory and if that helps.
I have both python 3 and python 2 as well. The problem I faced was even after successful pyperclip installation it was not working in Python 3.
The solution I got was to move to the directory of installation of Python 2
C:\Python27\Lib\site-packages
and copied the folders
pyperclip
and
pyperclip-1.7.0-py2.7.egg-info
to the python 3 directory which was in my case
C:\Users\MYNAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
I figured out what was wrong.
I also noticed that there are several posts all over in various locations of people reading the book
Automate the Boring Stuff with Python.
that are also struggling with this part.
After running
import sys
print('\n'.join(sys.path))
I started looking for differences between the output in command line and the output in the python shell. Everything looked extremely similar except in the directory:
lib/site-packages
I copied and pasted what was in this location, as pointed to in command line, to the location pointing to it in the python shell. I closed/reopened the shell and tried running the import pyperclip module, etc again and everything worked like a charm.
Problem is now solved.
I am using python 3.4 on windows 7. In order to open a doc file I am using this code:
import sys
import win32com.client as win32
word = win32.Dispatch("Word.Application")
word.Visible = 0
word.Documents.Open("MyDocument")
doc = word.ActiveDocument
I'M not sure why is this error popping up every time:
ImportError: no module named win32api
Although I have installed pywin32 from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
and I have also checked the path from where I am importing. I have tried reinstalling pywin32 as well but that doesn't remove the error.
Try to install pywin32 from here :
http://sourceforge.net/projects/pywin32/files/pywin32/
depends on you operation system and the python version that you are using. Normally 32bit version should works on both 32 and 64 bit OS.
EDIT: moved to https://github.com/mhammond/pywin32/releases
This is a bug in the library itself, probably they used a different python implementation for creating this.
What they are trying to import is the site-packages\win32\win32api.pyd file, but the win32 folder is not in the path that python searches in, but site-packages is.
Try to replace the import win32api (inside win32com\__init__.py) to from win32 import win32api
I encountered the same error yestoday with Python 3.6.1 on Windows 7, and resolved it by "pip install pypiwin32".
Had the same error trying to import win32com.client (using Python 2.7, 64-bit). I agree with TulkinRB, there seem to be path issues, but the fix suggested did not work for me, since I also could not import win32.
Perhaps my fix will also work in Python 3.4.
Eventually, installing the .exe from SourceForge as an administrator (as suggested in Rina Rivera's answer here) allowed me to import win32com.client from IDLE, but not when I executed the script I was originally trying to run.
In the end, I discovered 3 differences in the sys.path that had been extended when I installed as admin and opened IDLE, but were not applied when executing a script. By extending the sys.path in my script, I was able to get rid of the import errors when I executed it:
import sys
sys.path.extend(('C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin'))
Finally, if you want more than a temporary fix, the sys.path could be permanently extended by establishing IDLESTARTUP or PYTHONSTARTUP variables (as described here and here).
You can create the __init.py file inside the win32 folder and then go inside the win32com folder and change its __init__.py file, where it is import win32api, change to from win32 import win32api
I ended up debugging and copying and pasting the necessary files into the appropriate folders. It's a work-around until the bug is fixed, but it works.
from https://github.com/mhammond/pywin32/issues/1151#issuecomment-360669440
append the 'pypiwin32_system32' path to your system PATH,
in a script this can be done like:
import os
sitedir='C:/where_ever/'
os.environ["PATH"]+=(';'+os.path.join(sitedir,"pypiwin32_system32"))
...
from powershell
$env:PATH="$PATH;C:\where_ever\pywin32_system32";
python.exe ...
for help on site dir, see What is python's site-packages directory?
I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.
To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)