How to use system() command with administrator properties? - python-3.x

Say I have a simple program: (module being some module to import)
from os import system
try:
import module
except:
system('py -m pip install module')
import module
My problem is, the module that i need to install needs administrator privileges.
How do I launch py in administrator?
I have tried things like:
system('py',adminstrator), and stupid stuff like that hoping one of them to work, but to no avail.
Thanks!

You can follow the instructions here to create a sudo.cmd file, make it available in your system path, and then you can gain Administrator privileges in your Python script with:
system('sudo py -m pip install module')

Related

ImportError cannot import 'langauge' from 'google.cloud' (unknown location)

I'm trying to incorporate google-cloud-language to use sentiment analysis. I followed the guide on the google documentation website, like so:
from google.cloud import language
When I run the script I receive the following error:
ImportError cannot import 'langauge' from 'google.cloud' (unknown location)
I try to run the script like so:
python3 scriptName.py
I've installed google-cloud-language like so:
pip3 install gooogle-cloud-language
pip install google-cloud-language
I've done that in a virtual environment and outside a virtual environment.
Nothing works. How do I properly set up my script to successfully import the NLP module from google cloud?
Happy to see that you resolved your issue. However, I would like to point some things out:
In the error that you posted in the description there is a typo. It looked for a module named langauge instead of language.
pip3 install gooogle-cloud-language and python3 -m pip install google-cloud-language should be equivalent but that may not always be the case. For example when they are not in the same path. Check this answer for more details.
As I was writing this question and going through other similar questions on stackoverflow, I found another command to try and it seems to have worked.
python3 -m pip install google-cloud-language
I did this inside my virtualenv. It could also work outside of one, but because I got it working, I did not test the outside virtualenv case.

Python3 not recogninzing face_recognition as a module

I am trying to use the implement the face_recognition module, so far without success. When I try to run it with an import in python it just says that there is no such module, however when i just try to run face_recognition on it's own it works just fine.
Case 1: Same directory name
Maybe your main folder name is face_recognition. If that's the case, then try to change it because if you try to import it, the python will import it from your main folder rather than importing it from the package folder.
Case 2: Permission
If you're using Linux, then make sure that the package folder is accessible. To verify that this the issue, try to run the program with elevated privileges using sudo command. You can try using the --user flag with pip (pip install face_recognition --user).
Case 3: Environment variables
It might be the case that your environment variables are not updated. May reference to the package folder is not present. That's why your python interpreter is unable to find the package. For path details:
import sys
print(sys.path)
Using this, you will get an idea of whether your interpreter searches for the package or not.

Kivy Installation Guide for Windows 10

I've been trying to follow online youTube videos to install kivy on my Windows 10 computer (python-3.7.5-amd64, kivy 1.11.1). Aside from the fact that they seem to have different variations on how they approach the topic, I am unable to get a solution that operates satisfactorily.
These are the steps I am following:
I install python (python-3.7.5-amd64.exe) to C:\Python37
I modify the path to include to include the following: C:\Python37\Scripts;C:\Python37;C:\Python37\Libs;C:\Python37\DLLs;C:\Python37\Lib\site-packages;
I added the following environment variable PYTHONPATH = C:\Python37\DLLs;C:\Python37\Libs;C:\Python37;C:\Python37\Scripts;C:\Python37\Lib\site-packages;
I open a command window and type in the following commands (taken from kivy.org)
python -m pip install --upgrade pip wheel setuptools virtualenv
python -m pip install docutils pygments pypiwin32 kivy_deps.sdl2==0.1.* kivy_deps.glew==0.1.*
python -m pip install kivy_deps.gstreamer==0.1.*
python -m pip install kivy_deps.angle==0.1.*
python -m pip install kivy==1.11.1
python -m pip install kivy_examples==1.11.1
I try to run a simple program. From within Windows Explorer I right click the code file (label.py) and from the shortcut menu select python.
A windows pops up for an instant and a directory called __pycache__ gets created with kivy.cpython-37.pyc. Double clicking that causes the program to run.
Is it possible to have a easier solution in which the source code, once compiled executes?
If I open a command prompt and attempt to execute the source code using the command python label.py I get the following:
Traceback (most recent call last):
File "label.py", line 1, in <module>
from kivy.app import App
File "C:\Users\chrib\Google Drive\_Software\Python_Kivy\kivy.py", line 1, in <module>
from kivy.base import runTouchApp
ModuleNotFoundError: No module named 'kivy.base'; 'kivy' is not a package
Why should this happen?
Also is it possible to have a cleaner development environment. I am used to Visual Studio IDE and it would be great if I can use this environment.
Thanks
Code for label.py
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text='Hello world!');
if __name__=='__main__':
MyApp().run();
I've been trying to follow online youTube videos to install kivy on my Windows 10 computer
Have you tried simply following the instructions on kivy.org? There's no need to use youtube videos, the installation is largely a normal python module install.
I try to run a simple program. From within Windows Explorer I right click the code file (label.py) and from the shortcut menu select python.
Don't do this, run the file by opening a command prompt and typing python yourfilename.py. That way you will see the full traceback for any errors that occur.
A windows pops up for an instant and a directory called pycache gets created with kivy.cpython-37.pyc. Double clicking that causes the program to run.
It sounds likely that the first run is crashing. As above, you want to get the information about why.
Is it possible to have a easier solution in which the source code, once compiled executes?
When you run the code it does execute. As above, it's probably crashing.
ModuleNotFoundError: No module named 'kivy.base'; 'kivy' is not a package
Have you made a file named kivy.py? It looks likely that you have, and that this file is being imported in preference to the installed kivy module.
Also is it possible to have a cleaner development environment. I am used to Visual Studio IDE and it would be great if I can use this environment.
I'm not sure what you consider unclean about your development environment, but you should think in terms of python environments and their installed packages. Kivy is just a python module that you install into a python environment. When you use an IDE, it may integrate with one or more python environments (with options to switch between them). There's nothing special about using Visual Studio with Kivy, just do whatever you normally do to use it with Python.
I figured it out. I had a program in the code directory called kivy.py. I renamed that and it worked.

Is it possible to install python libraries with pip programmatically?

Let me explain what I want to do.
The list of libraries I want installed is listed in a .txt file.
My script reads the list from the file sequentially, and if the script isn't installed, it installs it via pip, or if it is already installed, checks the version and updates it if necessary.
I googled it up but didn't find how to do that. Can you offer any help or guidance?
Yes you can. Try this, here is an example of one module which is hard coded
import os
import subprocess
import sys
get_pckg = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in get_pckg.split()]
required_packeges = ['shopifyAPI'] // Make a change here to fetch from file
for packg in required_packeges:
if packg in installed_packages:
pass
else:
print('installing package')
os.system('pip install ' + packg)
First i will fetch all installed modules and then i will check my required module is installed or not if not then it will install it.
Yes, you can. Python module os does support running script programmatically. Since I don't know how your file structure looks like, I guess you can read the file and run the script sequentially.
import os
os.system("pip install <module>")
Use Following to install lib. programmatically.
import pip
try:
pip.main(["install", "pandas"])
except SystemExit as e:
pass

Pip in python34 does not work properly (OS Windows)

I'm writing a data extraction from xls python script using xlrd. I'm having trouble however with pip, I just managed to get a positive reply from the cmd when i requested (I had some trouble with it at first, but a few posts helped out)
py -m install requests
The response I now get is: "Requirement already satisfied." (with some mention of update since my pip version is 6.0.8 and there's a 7.0.1 available)
However, when I run my code part of which can be seen below I get a syntax error.
import xlrd
from xlrd import open_workbook
import xlwt
workbook = xlrd.open_workbook('editable.xls')
The error:
File "C:\Python34\lib\site-packages\xlrd__init__.py", line 1187
print "EXTERNSHEET(b7-):"
^ SyntaxError: invalid syntax
I run your program on Ubuntu/Linux and it runs fine.
I will recommend to follow my instructions for installing the xlrd on your Windows machine.
From here download the latest version of the xlrd.
Then extract the file, and using the command prompt in the new xlrd directory type:
python3 setup.py install
Unfortunately I don't own a Windows machine so I can't guaranteed to you that this is the right way to install it.
The trick was that you should run the setup.py using the python3 and not the python
(note that on my machine, I run the above command using sudo, which means in Linux administration permissions, so if you have any problems try to run your command prompt with administration permissions.)
I test this code on my Ubuntu machine:
import xlrd
from xlrd import open_workbook
workbook = xlrd.open_workbook('Untitled.xls')
I remove the import xlwt as I don't have this module, please try and let me know.
Edit: Also you will need the setuptools installed on your computer.

Resources