How to restart windows service through Python script? - python

I am creating a script to restart windows service through Python script but getting the below error:
Script error
Code:
import win32serviceutil
class serviceRestart:
serviceName = "MySQL57"
win32serviceutil.RestartService(serviceName)
if __name__ == '__main__':
lm = serviceRestart()

The error message pretty much says it all. You need to install the module.
http://sourceforge.net/projects/pywin32/files/pywin32/
If you have pip installed then you can use "pip install pypiwin32"

Related

Can't connect python to looker

I've downloaded the looker_sdk for python.
Wrote a very simple program:
from looker_sdk import client, models
def test_looker():
sdk = client.setup("./looker.ini")
if __name__ == "__main__":
test_looker()
However, when I'm running it I'm getting the error:
ImportError: cannot import name 'client' from 'looker_sdk'.
I do see the models and was able to perform:
sdk = looker_sdk.init31()
what am I missing?
Thanks,
Nir.
It seems there may be a missing client.py file in looker_sdk version 0.1.3b8, or in your installation - I tested this on 0.1.3b4 and found no such issue.
I recommend that you uninstall the package and re-install the latest version from PyPI.

Why do I keep getting a No Module Found: Spotipy error after installing it?

When I try to run a python program with Bash (Windows) I get the error "ModuleNotFoundError:No module named 'Spotipy'" Is there something I am doing wrong?
I have already tried installing the Spotipy packet using pip and easy install on Bash but I still get the error. I have also downloaded the packet on the file directory ,not sure if that makes a difference, still got the same error.
import os
import sys
import json
import spotipy
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
#Get the username from terminal
username = sys.argv[1]
#Erase cahce and prompt for user permission
try:
token = util.prompt_for_user_token(username)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username)
#Create spotifyObject
spotifyObject = spotipy.Spotify(auth=token)
This program is supposed to run on my terminal. Send me to a spotify login page then redirect me to a URL already set through a Spotify API.
install it using git: git clone https://github.com/plamere/spotipy.git
then go to the directory of the package and run: python setup.py install

how to import a python module before installing it?

So I'm trying to create a setup.py file do deploy a test framework in python.
The library has dependencies in pexpect and easy_install. So, after installing easy_install, I need to install s3cmd which is a tool to work with Amazon's S3.
However, to configure s3cmd I use pexpect, but if you want to run setup.py from a fresh VM, so we run into an ImportError:
import subprocess
import sys
import pexpect # pexpect is not installed ... it will be
def install_s3cmd():
subprocess.call(['sudo easy_install s3cmd'])
# now use pexpect to configure s3cdm
child = pexpect.spawn('s3cmd --configure')
child.expect ('(?i)Access Key')
# ... more code down there
def main():
subprocess.call(['sudo apt-get install python-setuptools']) # installs easy_install
subprocess.call(['sudo easy_install pexpect']) # installs pexpect
install_s3cmd()
# ... more code down here
if __name__ == "__main__":
main()
I know of course I could create a another file, initial_setup.py to have easy_install and pexpect installed, before using setup.py, but my question is: Is there a way to import pexpect before having it installed? The library will be installed before using it, but does the Python interpreter will accept the import pexpect command?
It won't accept it like that, but Python allows you to import things everywhere, not only in the global scope. So you can postpone the import until the time when you really need it:
def install_s3cmd():
subprocess.call(['easy_install', 's3cmd'])
# assuming that by now it's already been installed
import pexpect
# now use pexpect to configure s3cdm
child = pexpect.spawn('s3cmd --configure')
child.expect ('(?i)Access Key')
# ... more code down there
EDIT: there is a peculiarity with using setuptools this way, since the .pth file will not be reloaded until Python relaunches. You can enforce reloading though (found here):
import subprocess, pkg_resources
subprocess.call(['easy_install', 'pexpect'])
pkg_resources.get_distribution('pexpect').activate()
import pexpect # Now works
(Unrelated: I'd rather assume that the script itself is called with the needed privileges, not use sudo in it. That will be useful with virtualenv.)

ImportError: No module named netifaces

I'm trying to get the IP address and MAC address of my pc's network card by python. I got some code from here
I create a proj "getip".
create "main.py". And I modify the code of "main.py" as follow
from netifaces import interfaces, ifaddresses, AF_INET
def ip4_addresses():
ip_list = []
for interface in interfaces():
for link in ifaddresses(interface)[AF_INET]:
ip_list.append(link['addr'])
return ip_list
def main():
print ip4_addresses()
if __name__ == "__main__":
main()
and I create "app.yaml"
application: getip
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
script: main.py
and when I run the main.py at console as "python main.py", I got the ip addresses.
and when I run as "dev_appserver.py getip", the server is setup. When I browse the page as localhost:8080, the web page is white screen and I got the following error at console.
from netifaces import interfaces, ifaddresses, AF_INET
ImportError: No module named netifaces
How can I solve the problem?
just install netifaces
pip install netifaces if you have pip installed, or download the source, unpack it run and python setup.py install
warning: this will install it globally on your system, so use caution, or use virtualenv
If you are using ubuntu:
sudo apt install python3-netifaces
Came here for the same question but in my case pip install would say that requirement is already satisfied. However:
pip uninstall netifaces && pip install netifaces
fixed it.
Leaving this here for posterity. Use sudo if you need to.
Actually, the problem here is you must be root when installed with pip, or it will not install globally. Therefore would not be able to find the module unless in the same directory or path as the module directory
so you need this:
sudo pip install netifaces
or on windows install with an elevated command prompt!
It seems that you have installed netifaces in your local development environment. But Google App Engine does not recognize it.
If you run your script with python main.py, Python interpreter will look for your libraries in the PYTHONPATH. GAE does not follow that rule.
To install a library in GAE, usually you just need to put the library module directory in the root of your app path(whee the app.yaml is). But I don't think Google will allow you to install libraries that can get hardware information in their PaaS for security reasons.
Updates:
Becaue you just need a web server to output the result, I recommend you to choose a simple, well documented, micro Python web framework, like Flask or bottle.
Installation:
pip install Flask or easy_install Flask
code:
from flask import Flask
from netifaces import interfaces, ifaddresses, AF_INET
app = Flask(__name__)
def ip4_addresses():
ip_list = []
for interface in interfaces():
for link in ifaddresses(interface)[AF_INET]:
ip_list.append(link['addr'])
return ip_list
#app.route("/")
def main():
return str(ip4_addresses())
if __name__ == "__main__":
app.run()
Run: python main.py

Import error in twilio

I am having the same problem as this thread regarding twilio-python:
twilio.rest missing from twilio python module version 2.0.8?
However I have the same problem but I have 3.3.3 installed. I still get "No module named rest" when trying to import twilio.rest.
Loading the library from stand alone python script works. So I know that pip installing the package worked.
from twilio.rest import TwilioRestClient
def main():
account = "xxxxxxxxxxxxxxxx"
token = "xxxxxxxxxxxxxxxx"
client = TwilioRestClient(account, token)
call = client.calls.create(to="+12223344",
from_="+12223344",
url="http://ironblanket.herokuapp.com/",
method="GET")
if __name__ == "__main__":
main()
but this does not work:
from twilio.rest import TwilioRestClient
def home(request):
client = TwilioRestClient(account, token)
Do you have any idea what I can try next?
I named a python file in my project twilio.py. Since that file was loaded first, then subsequent calls to load twilio would reference that file instead of the twilio library.
TLDR: just don't name your python file twilio.py
Check which versions of pip and python you are running with this command:
which -a python
which -a pip
pip needs to install to a path that your Python executable can read from. Sometimes there will be more than one version of pip like pip-2.5, pip-2.7 etc. You can find all of them by running compgen -c | grep pip. There can also be more than one version of Python, especially if you have Macports or brew or multiple versions of Python installed.
Check which version of the twilio module is installed by running this command:
$ pip freeze | grep twilio # Or pip-2.7 freeze etc.
The output should be twilio==3.3.3.
I hope that helps - please leave a comment if you have more questions.
This Worked For me : (Windows)
Python librarys are in G:\Python\Lib
(Python is installed at G:, it might be different for you)
Download Twilio from github at paste the library at >> G:\Python\Lib <<
import problem gone :)
I had the same issue and it drove me crazy. Finally I figured it out. When you get the error:
AttributeError: module 'twilio' has no attribute 'version'
Look 2 lines above and the error is telling you where it expects to find the twilio file. So I moved it from where it was to where it was asking it to be.
Installed to:
c:\users\rhuds\appdata\local\programs\python\python37-32\lib\site-packages
Moved it to:
Traceback (most recent call last):
File "", line 1, in
import twilio
File "C:\Users\rhuds\AppData\Local\Programs\Python\Python37-32\twilio.py", line 2, in
Now I can import twilio. Besides that, the only other thing I did was uninstall old versions of Python, but I don't think that really mattered.

Categories