I added some code snippets to python script and sent him to client. There is new library, does the the other side to install also package? pip install pywin32
code that added by me to script is;
import win32com.client
xl = win32com.client.Dispatch("Excel.Application") #instantiate excel app
wb = xl.Workbooks.Open(r'C:\Users\jay\Desktop\PythonInOffice\python_run_macro\macro.xlsm')
xl.Application.Run('macro.xlsm!Module1.macro1("Jay")')
wb.Save()
xl.Application.Quit()
From answers':
if I write the code below do I need to install sys and subprocess separetely also?
import subprocess
import sys
def install('pywin32'):
subprocess.check_call([sys.executable, "-m", "pip", "install", 'pywin32'])
Hope below code helps
Normally check for if package there if not installs it
import sys
import subprocess
import pkg_resources
required = {'pywin32'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
NOTE:- if proxy being used, it won't work. on check_call enter proxy details to install
Related
I am trying to use the google API client to download something every day for something but I have deduced that for some reason it won't let me import googleapiclient when I run it in crontab.
For example, if I run this in crontab
crontab: * * * * * python3 test.py >> cron.log
test.py:
print("Hello")
cron.log outputs:
Hello
But if I then once I import it so the file look like this
from googleapiclient import discovery
print("Hello")
then the cron.log looks like this:
Its just empty. I can not figure out why this is true. The google API client I believe is installed correctly because when I run it manually then it works perfectly with no issues.
The operating system I am using is macos.
The way I fixed it is first by changing the crontab to python3 test.py > cron.log 2>&1. This showed that the errors where. Then I realized that it was not able to import googleapiclient into python. I still did not understand why it does not, but a workaround I found was to first install the libraries into a folder and then accessing the libraries from there.
To install the libraries into a folder the command I used in terminal is below:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib -t ./lib
Then inside the python I added whats below to the beginning of the file so it would know where to import the libraries from.
import os
import sys
file_path = os.path.dirname(__file__)
module_path = os.path.join(file_path, "lib")
sys.path.append(module_path)
This does work.
I'm trying to implement the answer here to suppress the chromedriver.exe prompt:
https://stackoverflow.com/a/39937466/264975
which tells me to use the following:
from win32process import CREATE_NO_WINDOW
However, I cannot get win32process module to load. I am told that it requires pypiwin32, however, there is no information on how to use these modules? For instance, what am I actually supposed to import and from where?
I successfully installed pypiwin32 using pip however, I have no idea how to verify it is working due to the lack of help files.
Would be grateful for some pointers as to how to get the example working.
Does it matter that im on a 64 bit pc? I think the python I am using is 32 bit though.
Was trying to do the same thing.
Am on a Windows 10 64-bit machine using Python 2.7.
Kept on saying win32process not found.
I installed a bunch of different modules and a few command line install commands, but what got it working was after I installed this exe package pywin32-221.win-amd64-py2.7.exe from https://sourceforge.net/projects/pywin32/files/pywin32/
Then as https://stackoverflow.com/a/39937466/264975 instructs go to your Python folder, then
Lib\site-packages\selenium\webdriver\common\
and edit service.py (in the thread it mentions services.py but this is what was in my folder)
And include from win32process import CREATE_NO_WINDOW at the top of this script. Mine looks like this
import errno
import os
import platform
import subprocess
from subprocess import PIPE
from win32process import CREATE_NO_WINDOW
import time
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common import utils
then further down in this script look for def start(self):, and just add this to the end of self.process =
creationflags=CREATE_NO_WINDOW
So mine looks like this
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=CREATE_NO_WINDOW)
That's all. Now the chromedriver.exe console does not pop-up at all in my python scripts.
That's what worked for me. Hard to say if it was a number of things together that made it work or just by installing the pywin32-amd64.exe package.
I am want to transform a hive table (hdfs spot instances) using a Python UDF for which I need an external library "user-agents". My udf without the use of external library is working fine. But I am not able to get things working when I want to use it.
I tried installing the library using the code itself given below.
import sys
import subprocess
import pip
import os
sys.stdout = open(os.devnull, 'w+')
pip.main(['install', '--user', 'pyyaml'])
pip.main(['install', '--user', 'ua-parser'])
pip.main(['install', '--user', 'user-agents'])
sys.stdout = sys.__stdout__
and after this I tried this
import user_agents
but the udf is crashing with an exception "No module found". I also tried checking the following paths through code :
/usr/local/lib/python2.7/site-packages
/usr/local/lib64/python2.7/site-packages
But no user_agents module was there. Any help on how to do it to get things working ? Would really appreciate it. Thanks !
I figured a way out of this. For those who are solving this same UDF issue and are not successful yet can possibly try this solution and check if it works for them too.
For external libraries, do the following steps:
Step 1: Force pip to install the external library through code itself to the current working directory of your UDF.
import sys
import os
import pip
sys.stdout = open(os.devnull, 'w+')
pip.main(['install', 'user-agents', '-t', os.getcwd(), '--ignore-installed'])
sys.stdout = sys.__stdout__
Step 2: Update your sys.path
sys.path.append(os.getcwd())
Step 3: Now import the library :)
from user_agents import parse
That's it. Please check and confirm it this works for you too.
I want to write a script to automatically setup a brand new ubuntu installation and install a django-based app. Since the script will be run on a new server, the Python script needs to automatically install some required modules.
Here is the script.
#!/usr/bin/env python
import subprocess
import os
import sys
def pip_install(mod):
print subprocess.check_output("pip install %s" % mod, shell=True)
if __name__ == "__main__":
if os.getuid() != 0:
print "Sorry, you need to run the script as root."
sys.exit()
try:
import pexpect
except:
pip_install('pexpect')
import pexpect
# More code here...
The installation of pexpect is success, however the next line import pexpect is failed. I think its because at runtime the code doesn't aware about the newly installed pexpect.
How to install and import Python modules at runtime? I'm open to another approaches.
You can import pip instead of using subprocess:
import pip
def install(package):
pip.main(['install', package])
# Example
if __name__ == '__main__':
try:
import pexpect
except ImportError:
install('pexpect')
import pexpect
Another take:
import pip
def import_with_auto_install(package):
try:
return __import__(package)
except ImportError:
pip.main(['install', package])
return __import__(package)
# Example
if __name__ == '__main__':
pexpect = import_with_auto_install('pexpect')
print(pexpect)
[edit]
You should consider using a requirements.txt along with pip. Seems like you are trying to automate deployments (and this is good!), in my tool belt I have also virtualenvwrapper, vagrant and ansible.
This is the output for me:
(test)root#vagrant:~/test# pip uninstall pexpect
Uninstalling pexpect:
/usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/ANSI.pyc
/usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/FSM.pyc
/usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/fdpexpect.pyc
/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg-info
/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.pyc
/usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/pxssh.pyc
/usr/lib/python-environments/test/lib/python2.6/site-packages/screen.py
/usr/lib/python-environments/test/lib/python2.6/site-packages/screen.pyc
Proceed (y/n)? y
Successfully uninstalled pexpect
(test)root#vagrant:~/test# python test.py
Downloading/unpacking pexpect
Downloading pexpect-2.4.tar.gz (113Kb): 113Kb downloaded
Running setup.py egg_info for package pexpect
Installing collected packages: pexpect
Running setup.py install for pexpect
Successfully installed pexpect
Cleaning up...
<module 'pexpect' from '/usr/lib/python-environments/test/lib/python2.6/site-packages/pexpect.pyc'>
(test)root#vagrant:~/test#
For those who are using pip version greater than 10.x, there is no main function for pip so the alternative approach is using import pip._internal as pip instead of import pip like :
Updated answer of Paulo
import pip._internal as pip
def install(package):
pip.main(['install', package])
if __name__ == '__main__':
try:
import pexpect
except ImportError:
install('pexpect')
import pexpect
I actually made a module for this exact purpose (impstall)
It's really easy to use:
import impstall
impstall.now('pexpect')
impstall.now('wx', pipName='wxPython')
Github link for issues/contributions
I solved my problem using the imp module.
#!/usr/bin/env python
import pip
import imp
def install_and_load(package):
pip.main(['install', package])
path = '/usr/local/lib/python2.7/dist-packages'
if path not in sys.path:
sys.path.append(path)
f, fname, desc = imp.find_module(package)
return imp.load(package, f, fname, desc)
if __name__ == "__main__":
try:
import pexpect
except:
pexpect = install_and_load('pexpect')
# More code...
Actually the code is less than ideal, since I need to hardcode the Python module directory. But since the script is intended for a known target system, I think that is ok.
I had the same issue but none of Google's searches helped. After hours debugging, I found that it may be because the sys.path is not reloaded with new installation directory.
In my case on my Ubuntu Docker, I want to import dns.resolver at runtime for Python3.8 (pre-installed). I also created ubuntu user and run all things with this user (including my Python script).
Before installing, sys.path doesn't have /home/ubuntu/.local/lib/python3.8/site-packages since I didn't install anything.
While installing with subprocess or pip.main like above, it creates /home/ubuntu/.local/lib/python3.8/site-packages (as user installation).
After installing , the sys.path should be refreshed to include this new location.
Since the sys.path is managed by site module, we should reload it (ref HERE):
import site
from importlib import reload
reload(site)
The full block for anyone that needs:
import subprocess
import sys
try:
import dns.resolver
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "dnspython"])
import site
from importlib import reload
reload(site)
import dns.resolver
I'm not Python developer so these code can be simplify more. This may help in cases such as fresh CI/CD environment for DevOps engineers like me.
I'm trying to run a psutil command remotely:
import os, sys, time
import rpyc
import psutil
command = """def rpcexecute():
import psutil
cpu = psutil.cpu_percent(interval=1)
return cpu"""
conn = rpyc.classic.connect('192.168.0.100')
conn.execute(command)
remote_exec = conn.namespace['rpcexecute']
result = remote_exec()
I've got an error: "ImportError: No module named psutil", but I have installed psutil in the both (remote and localhost).
When I test using import os for example, it works fine.
Some tip?
Thank you.
Solved!
I've restarted the rpyc classic service on the server and try again.
Now it works fine.
BR,
Junix