So I was hoping to write my code more resiliently, starting from the top in python I was thinking of addressing importing.
I want code to run on systems where required packages haven't been installed. To achieve this I was hoping to install packages on the run with python.
try:
import pygame as pg
except(ImportError):
# [install pygame][1] here
# Download and run pygame.MSI (windows)
# apt-get install python-pygame
install pygame
From this specific solution I intend to make a more generic function ...
import subprocess as sp
def imp(inP,name,location):
try:
exec "import "+inP+" as "+(name if name != "" else "")
except ImportError:
try:
os = ????
if(os == windows):
sp.call("pip install "+location,shell=True)
if(os == unix):
sp.call("sudo apt-get install python-"+inP,shell=True)
r = True
except Exception:
print colPrt("ERROR installing ") + inP
r = False
try:
exec "import "+inP+" as "+(name if name != "" else "")
except(ImportError):
print colPrt("ERROR importing ") + inP
r = False
return r
and so my one question becomes 2. The first being the best practicing for installing modules on the run and the second being how that differs between a unix and windows environment.
Ps, colPrt simply returns red text to the terminal
def colPrt(s):
return("\x1B["+"31;40m" + str(s) + "\x1B[" + "0m")
thanks for your thoughts : )
The first being the best practicing for installing modules on the run
I would advise against installing modules on the run for a couple reasons:
Users should have a choice about whether they want to install stuff. Most users aren't interested in making that choice at runtime (and your program shouldn't be running with the necessary privileges to install stuff like that anyway)...
Follow up to the first point -- Dependencies should be installed when your package gets installed.
It'll clutter your code with a bunch of stuff that you really don't want to maintain.
Even if you do include it, whose to say that your code and the latest version of some third-party package on the web are compatible?
Python's standard build system has ways of interacting with dependencies, so when your package gets downloaded and installed (e.g. via easy_install or pip), then the dependencies should all come with it.
Take a look here for some advice on how to package your python code.
Try to look at the pip-accel, it is wrapper over pip, that is working cross platform. If you users have no python environment pre installed, I want recommend you to look at virtualenv. Pip + virtualenv works like a charm, you just have to write short python script that will run it with setups.
Anyway, If you will use your own solutions, it will be very difficult to support, maybe one day your boss come and ask you to add Mac Os support. Better to write you wrapper over pip+virtualenv than implement all logic by yourself.
For exception write out only error message and let the choice of installing modules on user. Or write it ower setup (install) wrapper.
On unix / linux platforms the python is distributed with easy_install and do not try nothing to install in background with apt-get. Without permissions would not succeed. Needless.
And we used not only Debian based linux :)
eg. my standard OS is unix based: QNX :)
Related
When i used to code pynotify for python2.7 in Windows 10 then it shows an error that module object has no attribute 'init'. And i have already done to install the init module but same error occur. so what are the possible solutions for this problem?
here the code is.
import pynotify
pynotify.init("Basic")
n = pynotify.Notification("Title","Some sample content")
n.show()
It looks like the pynotify package you are looking for was initially designed for linux and was never supposed work on windows
The pynotify package installable through pip is just a name collision, and is not what not what you are looking for. (as this stackoverflow thread shows : I have already installed pynotify, still getting error no module named pynotify)
The package you want has GTK dependencies and can be installed in linux-based systems through the package management system using commands like : sudo apt-get install pynotify.
Other similar packages like notify2 (https://pypi.python.org/pypi/notify2) require linux components like dbus, and thus are not windows compatible.
If you're looking for a way to make windows 10 notifications, you can probably have look at this repository https://github.com/jithurjacob/Windows-10-Toast-Notifications.
It took me forever to find this solution, so I want others to be able to see it.
I wanted to write a python script to create a virtual env and install modules inside it. Unfortunately, pip does not play nice with subprocess, as detailed here:
https://github.com/pypa/pip/issues/610
My answer is already on that thread, but I wanted to detail it below
Basically, the issue is that pip is still using the python executable that the original python called. To fix this, you need to delete it from the passed in environment variables. Here is the solution:
#!/usr/bin/python3
import os
import subprocess
python_env_var = {"_", "__PYVENV_LAUNCHER__"}
CMD_ENVIRONMENT = {name: value for (name, value) in os.environ.items()
if name not in python_env_var}
subprocess.call('./pip install -r requirements.txt', shell=True,
env=CMD_ENVIRONMENT)
Tested on Mac, ubuntu 14.04 and Windows with python 3
This same problem could easily exist for lots of situations -- I will be deleting this variable from now on, to prevent this kind of behavior when dealing with virtualenv's
First of all, I should say, that I checked all links at stackoverflow, but I still can't make it work. What I want is just as simple as my nose - I want to import numpy and I want to import modules created by f2py. Now, when I do in console
$ python
>> import numpy
I get an error No module named numpy. In the same way I get an error, when I try to import a fortran module made by f2py:
>> import testmodule
My OS is Ubuntu 12.04. I should also add, that I tried to uninstall and reinstall python hundreds of times with different libs and of course I did sudo apt-get install python-numpy etc. But that didn't help. What I want to hear from you, guys, is a complete step-by-step instruction (including unintallation of the current versions of python which may be corrupt and including installation instructions - download this version, unpack it to here etc.) I guess that instruction will be extremely worthy and usefull for python newbeis like me. The problem that I face now seems to be the simplest in the world, but I wonder why it has no simple solution.
Does your Python prompt have >> as the prompt? I've always seen >>> from Python.
If uninstalling Python and reinstalling doesn't work, perhaps the problem is with your user account? I'd try:
Create a new user, sudo useradd joe
Log in as the new user sudo -u joe bash -login
See if Python and numpy work now.
Exit from joe's shell (exit, logout or ^D).
Get rid of joe, sudo userdel joe
Now at least you know if the problem is with your system setup or your user setup.
Other things to look for:
run pip freeze | grep numpy or pip freeze | less to see which numpy package is (or isn't) installed.
Do you have something odd in your environment? Try env | grep -i python to see if you have a nonstandard environment variable.
Do you have python aliased to something else in your .profile or other startup? Try alias python to see if you really are starting python when you run python.
Do you have some old python in your $PATH? You can try which python and you should see /usr/bin/python. If you get '/usr/local/bin/pythonthat should be a link pointing to the "real" python at/usr/bin/python`.
Have a look at /usr/bin/python. It should be a link to python2.7.
During your uninstall-reinstall cycles, you can run pip freeze to see the list of installed packages. You should be able to make numpy appear and disappear in the freeze list when you install and uninstall it.
I use Anaconda 1.7, 32 bit. I downloaded the correct version of the netCDF4 installer from here.
I attempted to copy the HKEY_LOCAL_MACHINE\SOFTWARE\Python folder into HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node. No luck.
Does anyone have any idea why this might be happening? Anaconda installed in the default location, C:/.
Yes, I know Anaconda has netCDF4 in the packages list - but if you look closely, it's only offered for Mac and Linux.
This error can occur if you are installing a package with a different bitness than your Python version. To see whether your Python installation is 32- or 64-bit, see here.
Some superpacks (e.g. for Scipy) available on SourceForge or python.org are for 32-bit systems and some are for 64-bit systems. See this answer. In Windows, uninstalling the 32-bit and installing the 64-bit version (or vice versa if your installation is 32-bit) can solve the problem.
I had the same issue when using an .exe to install a Python package (because I use Anaconda and it didn't add Python to the registry). I fixed the problem by running this script:
#
# script to register Python 2.0 or later for use with
# Python extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig#python.org/msg10512.html
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __name__ == "__main__":
RegisterPy()
Try the steps described here:
http://avaminzhang.wordpress.com/2011/11/24/python-version-2-7-required-which-was-not-found-in-the-registry/
Just download Python 2.7.6 Windows Installer from the official Python download page, and launch the install package.
I think it really depends on why this error is given. It may be the bitness issue, but it may also be because of a deinstaller bug that leaves registry entries behind.
I just had this case because I need two versions of Python on my system. When I tried to install SCons (using Python2), the .msi installer failed, saying it only found Python3 in the registry. So I uninstalled it, with the result that no Python was found at all. Frustrating! (workaround: install SCons with pip install --egg --upgrade scons)
Anyway, I'm sure there are threads on that phenomenon. I just thought it would fit here because this was one of my top search results.
I had such problem. Solution was simple :
Install python 2.7 64bit version.
Export HKEY_LOCAL_MACHINE\SOFTWARE\Python.
Remove Python 2.7.
insert exported reg file.
rename all C:\Python27 to C:\Anaconda ( insert your path ).
P.S. Sorry, for bad grammar.
Check for the 32/64 bit you trying to install. both python interpreter and your app which trying to use python might be of different bit.
I'm just starting out with Python, and have found out that I can import various libraries. How do I find out what libraries exist on my Mac that I can import? How do I find out what functions they include?
I seem to remember using some web server type thing to browse through local help files, but I may have imagined that!
From the Python REPL (the command-line interpreter / Read-Eval-Print-Loop), type help("modules") to see a list of all your available libs.
Then to see functions within a module, do help("posix"), for example. If you haven't imported the library yet, you have to put quotes around the library's name.
For the web server, you can run the pydoc module that is included in the python distribution as a script:
python /path/to/pydoc.py -p 1234
where 1234 is the port you want the server to run at. You can then visit http://localhost:1234/ and browse the documentation.
Every standard python distribution has these libraries, which cover most of what you will need in a project.
In case you need to find out if a library exists at runtime, you do it like this
try:
import ObscureModule
except ImportError:
print "you need to install ObscureModule"
sys.exit(1) # or something like that
You can install another library: yolk.
yolk is a python package manager and will show you everything you have added via pypi. But it will also show you site-packages added through whatever local package manager you run.
just run the Python interpeter and type the command
import "lib_name"
if it gives an error, you don't have the lib installed...else you are good to go
On Leopard, depending on the python package you're using and the version number, the modules can be found in /Library/Python:
/Library/Python/2.5/site-packages
or in /Library/Frameworks
/Library/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages
(it could also be 3.0 or whatever version)...
I guess it is quite the same with Tiger
Considering that in every operating system most of python's packages are installed using 'pip' (see pip documentation) you can also use the command 'pip freeze' on a terminal to print a list of all the packages you have installed through it.
Other tools like 'homebrew' for macOS (used when for some reason you can't install a package using pip) have similar commands, in this specific case 'brew list'.