I tried to import the python lib called "redfish" but was in vain; I import another lib(os) first and seems ok:
>>> import os
>>> import redfish
$HOME environment variable not set, please check your systemPS
C:\Users\user>
I also checked the environment variable; I use Enthought Canopy as my python IDE:
And python is also added to system variable:
And then I used python console, tried to get HOME, but it shows nothing
I've searched for a while but no related issues when users import python redfish lib, maybe redfish is a new BMC standard so not widely-used currently for pyhton-users. Thanks in advance for any suggestions.
Finally, I found the path of redfish packages :
>>> import sys
>>> print '\n'.join(sys.path)
>>>..................................................
..................................................
C:\users\user\appdata\local\enthought\canopy\user
*C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages*
and then add it to the system variable $HOME, and open python shell again to import the lib and it works.
>>> import os
>>> HOME = os.getenv('HOME')
>>> HOME
'C:/users/user/appdata/local/enthought/canopy/user/lib/site-packages/'
>>> import redfish
>>> redfish.connect
<function connect at 0x0000000003E6B898>
Run the following in the terminal
pip uninstall python-redfish
pip uninstall python
pip install python
Related
I am using Windows 10 And I wrote this code
MEDIA_ROOT = os.path.dir(BASE_DIR , 'media')
then i tried to open the server of django the CMD=terminal said:
NameError: name 'os' is not defined)
Then I tried to add (import os) but it didn't work also then I added (from settings import PROJECT_ROOT) but it didn't work either so what is the problem?
First, did you wrote "import os".
# pip3 install os
import os
os.system("echo Hello World")
If you can't install os in pip, you pip is broken => try reinstall it!
If so, maybe your python is not on path. Perhabs you added the wrong path. Try to add it to sys-variables. Or the most unlikly case, os is not included, then try "pip3 install os".
I have a package that I would like to automatically install and use from within my own Python script.
Right now I have this:
>>> # ... code for downloading and un-targzing
>>> from subprocess import call
>>> call(['python', 'setup.py', 'install'])
>>> from <package> import <name>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named <package>
Then I can continue like this:
>>> exit()
$ python
>>> from <package> import <name>
And it works just fine. For some reason, Python is able to pick up the package just fine if I restart after running the setup.py file, but not if I don't. How can I make it work without having the restart step in the middle?
(Also, is there a superior alternative to using subprocess.call() to run setup.py within a python script? Seems silly to spawn a whole new Python interpreter from within one, but I don't know how else to pass that install argument.)
Depending on your Python version, you want to look into imp or importlib.
e.g. for Python 3, you can do:
from importlib.machinery import SourceFileLoader
directory_name = # os.path to module
# where __init__.py is the module entry point
s = SourceFileloader(directory_name, __init__.py).load_module()
or, if you're feeling brave that your Python path knows about the directory:
map(__import__, 'new_package_name')
Hope this helps,
I downloaded from seaborn from GitHub.
Through command prompt, cd to downloads\seaborn folder
python install setup.py
Then using spyder from anaconda, checked if it was installed by running the following in a console
import pip
sorted(["%s==%s" % (i.key, i.version)
for i in pip.get_installed_distributions()])
Seeing that it was not there, go to tools and select "Update module names list"
Again trying the previous code in a python console, the lib was still not showing.
Restarting Spyder and trying import seaborn worked.
Hope this helps.
I am wanting to use portable python 2.7.x to connect to an Access database. I can't seem to get it working as it doesn't have the pyodbc libraries. Is there another way to use portable python to connect?
The newest version of portable python has an option to install pyodbc but you have to select the option it doesn't go in by default.
Click on the modules option
Select the option for pyodbc
I have did it in different way.. .
follow what i have just done on my mac snow leopard!!
Download the the pyodbc's source from where it is on internet.
Extract and 'cd' into that dir.. . Run 'python setup.py build' and then take 'pyodbc.so' file from that build's dir. Make new python file named as 'pyodbc.py' and write the content given below.(and put that 'pyodbc.so' file with it)
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'pyodbc.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
(remember put above code in file named as 'pyodbc.py' and put that 'pyodbc.so' file with that)
and at last ..put all these where ever you want to use or in run time add that location into sys.path as:
>>> import sys
>>> sys.path.insert(0,"/my_portable/location") # location to dir which contains those two files
after doing all this i have put those two files with my test python file..and in that i am able to import 'pyodbc' without installing it.
>>> import pyodbc
>>> dir(pyodbc)
In Python, I'm getting an error because it's loading a module from /usr/lib/python2.6/site-packages but I'd like it to use my version in $HOME/python-modules/lib/python2.6/site-packages, which I installed using pip-python --install-option="--prefix=$HOME/python-modules --ignore-installed
How can I tell Python to use my version of the library? Setting PYTHONPATH to $HOME/python-modules/lib/python2.6/site-packages doesn't help, since /usr/lib/... apparently has precedence.
Take a look at the site module for ways to customize your environment.
One way to accomplish this is to add a file to a location currently on sys.path called usercustomize.py, when Python is starting up it will automatically import this file, and you can use it to modify sys.path.
First, set $PYTHONPATH to $HOME (or add $HOME if $PYTHONPATH has a value), then create the file $HOME/usercustomize.py with the following contents:
import sys, os
my_site = os.path.join(os.environ['HOME'],
'python-modules/lib/python2.6/site-packages')
sys.path.insert(0, my_site)
Now when you start Python you should see your custom site-packages directory before the system default on sys.path.
Newer Python versions now have built-in support to search the opendesktop location:
$HOME/.local/lib/pythonX.Y/site-packages
If you put your local modules there you don't have to any sys.path manipulations.
If one has multiple versions of a package installed, say e.g. SciPy:
>>> import scipy; print(scipy.__version__); print(scipy.__file__)
0.17.0
/usr/lib/python3/dist-packages/scipy/__init__.py
and one would like the user installed version (installed e.g. using pip install --user --upgrade scipy) to be prefered, one needs a usercustomize.py file in ~/.local/lib/python3.5/site-packages/ with e.g. this content:
import sys, os
my_site = os.path.join(
os.environ['HOME'], '.local/lib/python%d.%d/site-packages' % (
sys.version_info[0], sys.version_info[1]))
for idx, pth in enumerate(sys.path):
if pth.startswith('/usr'):
sys.path.insert(idx, my_site)
break
else:
raise ValueError("No path starting with /usr in sys.path")
(the for loop selecting index ensures that packages installed in "develop mode" takes precedence) now we get our user specific version of SciPy:
>>> import scipy; print(scipy.__version__); print(scipy.__file__)
0.18.1
/home/user/.local/lib/python3.5/site-packages/scipy/__init__.py
to prefer packages installed to userbase (e.g. pip install --user --upgrade cool_thing )
in ~/.bashrc,~/.profile, or whatever the init file for your shell is, add
export PYTHONUSERBASE="$HOME/python-modules"
in $PYTHONUSERBASE/usercustomize.py
#!/usr/bin/env python
import sys, site
sys.path.insert(0, site.getusersitepackages())
When trying to launch Mercurial(hg) after a restart in my Ubuntu 9.10 Linux Box I got following message:
abort: couldn't find mercurial libraries in [/usr/bin /usr/local/lib/python2.6/dist-packages/vipy-0.4-py2.6.egg /usr/local/lib/python2.6/dist-packages/nose-0.11.1-py2.6.egg /usr/local/lib/python2.6/dist-packages/rope-0.9.2-py2.6.egg /usr/local/lib/python2.6/dist-packages/Sphinx-0.6.3-py2.6.egg /usr/local/lib/python2.6/dist-packages/django_html-0.0.1-py2.6.egg /usr/local/lib/python2.6/dist-packages/html5lib-0.11.1-py2.6.egg /home/kenny /home/kenny/Projects/soclone-read-only /home/kenny/python/Django /home/kenny/python/pysmell /home/kenny/python/Django/ropemode /home/kenny/python/Django/rope /home/kenny/python/lib /usr/lib/python2.6 /usr/lib/python2.6/plat-linux2 /usr/lib/python2.6/lib-tk /usr/lib/python2.6/lib-old /usr/lib/python2.6/lib-dynload /usr/local/lib/python2.6/dist-packages]
(check your install and PYTHONPATH)
Mysteriously other Python programs don't find their modules, including django-admin, bzr, BUT surprisingly the Python interpreter itself is launching.
Here you can find my current sys.path:
['', '/usr/local/lib/python2.6/dist-packages/vipy-0.4-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/nose-0.11.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/rope-0.9.2-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Sphinx-0.6.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/django_html-0.0.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/html5lib-0.11.1-py2.6.egg', '/home/kenny', '/home/kenny/Projects/soclone-read-only', '/home/kenny/python/Django', '/home/kenny/python/pysmell', '/home/kenny/python/Django/ropemode', '/home/kenny/python/Django/rope', '/home/kenny/python/lib', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages']
Does anyone know how to resolve this issue?
I know this is no programming question in specific, but it disallows me to program, so I beg your comprehension!
Thanks in advance.
Try this:
update-python-modules -p
(might need to sudo that...)
Source:
http://hg.opensource.lshift.net/mercurial-server/rev/32dba1a70a54
All of the sites I've googled for this say that your PYTHONPATHis set wrong. The code that you are hitting in mercurial appears to be this:
try:
from mercurial import demandimport; demandimport.enable()
except ImportError:
import sys
sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
' '.join(sys.path))
sys.stderr.write("(check your install and PYTHONPATH)\n")
sys.exit(-1)
So where is demandimport on your machine? On my windows box, it is here:
>>> from mercurial import demandimport
>>> demandimport.__file__
'C:\\Python26\\lib\\site-packages\\mercurial-1.4.1-py2.6-win32.egg\\mercurial\\demandimport.pyc'
And that works because I have mercurial in my PYTHONPATH:
>>> import sys
>>> for s in sys.path:
... print s
...
# Other crud deleted...
C:\Python26\lib\site-packages\mercurial-1.4.1-py2.6-win32.egg
C:\Windows\system32\python26.zip
C:\Python26\DLLs
C:\Python26\lib
C:\Python26\lib\plat-win
C:\Python26\lib\lib-tk
C:\Python26
C:\Python26\lib\site-packages
Your PYTHONPATH makes no mention of mercurial. At a guess, I would add this to your PYTHONPATH:
/usr/local/lib/python2.6/site-packages
and I would re-install mercurial from make. That advice worked well here.
Edit: And on my Ubuntu 9.10 box, I got these results:
>>> import mercurial
>>> mercurial.__file__
'/usr/lib/pymodules/python2.6/mercurial/__init__.pyc'
>>> import sys
>>> for s in sys.path:
... print s
...
/usr/local/lib/python2.6/dist-packages/pip-0.6.1-py2.6.egg
/usr/local/lib/python2.6/dist-packages/virtualenv-1.4.3-py2.6.egg
/usr/lib/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/usr/lib/python2.6/lib-old
/usr/lib/python2.6/lib-dynload
/usr/lib/python2.6/dist-packages
/usr/lib/python2.6/dist-packages/PIL
/usr/lib/python2.6/dist-packages/gst-0.10
/usr/lib/pymodules/python2.6
/usr/lib/python2.6/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.6/gtk-2.0
/usr/local/lib/python2.6/dist-packages
/usr/local/lib/python2.6/dist-packages/PIL
And this makes me think the problem is that this is missing for you: /usr/lib/pymodules/python2.6.
Is mercurial located in one of the library installation paths (dist-packages or site-packages)? You can use the find tool to look for it?
Did you have luck installing small libraries and access them from Python on this machine?
Thanks for the effort to all of you.
I've solved the problem thanks to hughdbrown. hughdbrown, you've made me realize that I commited a typo when defining doing $PYTHONPATH; instead of adding the path /usr/lib/pymodules/python2.6, I wrote /usr/lib/pymodules/ so python couldn't import the libraries... However, I corrected it and I'm glad to see Mercurial and Co. work again.
The only strange thing is, WHY it changed... Well, I'll be knowing from now on what to do.
You could try to reinstall affected Python programs with aptitude:
sudo aptitude reinstall mercurial