Error setting environment variables in python - python

I am trying to use Bloomberg python API. I need to set BLPAPI_ROOT environment variable for this. I added,
export BLPAPI_ROOT="/home/user/Downloads/blpapi_cpp_3.6.3.1"
export PATH=$PATH:$BLPAPI_ROOT
to my .bashrc file and ran source .bashrc.
Now, when I open python shell and do,
print os.environ['BLPAPI_ROOT']
it gives me correct output. But when the same this runs inside the setup.py provided, it throws a
Traceback (most recent call last):
File "setup.py", line 27, in <module>
blpapiRoot = os.environ['BLPAPI_ROOT']
File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'BLPAPI_ROOT'
What am I missing here ?
System :
Ubuntu 12.04
Python 2.7

I would try using it the following way:
import os
try:
os.environ['BLPAPI_ROOT'] = "/home/user/Downloads/blpapi_cpp_3.6.3.1"
except EnvironmentError:
sys.exit(1)

This is quite old, but for anyone searching, you can get around this by setting sudo to keep the environmental variable BLPAPI_ROOT, a la keep environmental variables using sudo.
sudo visudo
Then add:
Defaults env_keep +="BLPAPI_ROOT"
You can now run:
sudo python setup.py install
and it should work just fine.

Related

Error with TensorFlow MNIST [duplicate]

Since updating from Homebrew Python 2.7.11 (from 2.7.10) I'm suddenly unable to test register my package on PyPi from the PyCharm IDE console.
Running (as an "External Tool")
python -B setup.py register -r pypitest
I now get
Traceback (most recent call last):
File "setup.py", line 22, in <module>
from setuptools import setup
File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>
from setuptools.extension import Extension
File "/usr/local/lib/python2.7/site-packages/setuptools/extension.py", line 8, in <module>
from .dist import _get_unpatched
File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 16, in <module>
from setuptools.depends import Require
File "/usr/local/lib/python2.7/site-packages/setuptools/depends.py", line 6, in <module>
from setuptools import compat
File "/usr/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Process finished with exit code 1
I'm not sure how to proceed. I only get this issue if I execute from within my IDE's console. If I do it directly at the system command line (Terminal on OS X) I have no problems.
OS X 10.11.3; Homebrew Python 2.7.11; PyCharm 5.0.3
tl;dr: Fix this issue by doing one of the following:
type hash -r python, OR
log out and log in.
EDIT: An answer to my related question makes it clear what's happening here. When you install a new version of python, you may need to run hash -r python to tell bash to reset the "cached" location to the python executable.
In my case, I was typing python, which was on my $PATH at /usr/local/bin/python. But bash was still using the old cache location /usr/bin/python. So, the old executable was called, but the new path was provided to python in sys.argv[0]. This means that the old executable was running, but the new sys.executable value caused all the wrong modules to get loaded (including the io module).
I'm having the same problem. I installed python 2.7.11 via an installer from Python.org. Strangely, the issue seems to be related to some subtle difference between how OSX launches python when I invoke it from the shell using the full path vs. using just the word python.
So, for me, this works (invoking python via the full path /usr/local/bin/python):
$ which python
/usr/local/bin/python
$ /usr/local/bin/python -c "import io"
$
... but this doesn't:
$ python -c "import io"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
So, as a workaround, you can try doing the same thing.
Elsewhere, I've posted a separate question about this puzzling behavior. Maybe somehow merely calling python invokes some strange mix of the 2.7.11 executable with the 2.7.10 dylibs??
According to https://github.com/klen/python-mode/issues/634:
I had the same issue, but successfully fixed. In my case I compiled
python and vim with homebrew, when PYTHON_PATH has been specified and
set to one of my dev environments, where I also had some libraries,
including io. Workaround was simple: open new terminal, make sure that
you do not have custom PYTHON_PATH, uninstall python, uninstall vim.
Reinstall both of them.
and
Problem solved.
Culprit is the update from python 2.7.10 to 2.7.11.
If you are using conda package control, simply run "conda install
python=2.7.10" will solve this problem.
This doesn't give the root cause though. Since this happens with _io, this looks like a bug in python 2.7.11 (unlikely, there would be a world-scale outcry and a prompt fix if it was) or some packaging bug or version mismatch specifically with the homebrew version (and maybe some related ones, too).
Try to import _io in the console and if it succeeds, check if it was loaded from the same path.
Reinstall python.
brew unlink python && brew reinstall python
Secure the path
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/
BACKUP and Change the order of "paths" file.
sudo nano /etc/paths
it seems, the order of paths, it is decisive to run python properly. In my case, the result was:
#sudo nano /etc/paths
/usr/bin
/usr/local/bin
/bin
/usr/sbin
/sbin
On my mac, path is like this.
$ which python
/usr/local/bin/python
Now I can run both:
$ /usr/local/bin/python -c "import io"
$ python -c "import io"
I had the same issue, it is successfully fixed by just replacing the _io.so file.
sudo find / -name _io.so
copy the path of the _io.so file which DOES NOT belong to python-2.7.11. For example, copy the path of _io.so which is under python-2.7.5:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Replace the /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so file with the _io.so that you just found.
This happened to me as well in MacVim. I solved it by making sure :python print(sys.path) is using system Python (e.g. /Library/Python/2.7/...)
Since I installed MacVim via Homebrew, I just did that by:
Spawn a new shell that had which python -> /usr/bin/python. For my case I needed to remove the pyenv line from my .bash_profile. If you installed Python via Homebrew you may want to brew unlink python first
brew reinstall macvim
If your problem is caused by anaconda, it is unnecessary to remove //anaconda directory.
Just open your ~/.bash_profile, find the line
export PATH="//anaconda/bin:$PATH
and comment it out, then restart your terminal session.
Another quick workaround if you don't mind sticking with Python 2.7.10 is to specify the path of the Python interpreter executable that will be used for the virtualenv. On OSX that path is usually /usr/bin/python:
virtualenv venv --python=/usr/bin/python
Can't add comment (?) so this just to share my exp., downgrade to 2.7.10 works fr me.
I got this error after a failed NLTK download, I needed to uninstall anaconda:
sudo rm -rf ~/anaconda
update PATH variable
This happened when I already had tried to create a venv in a folder, and mistakenly was trying to initialize a second one! So I just removed venv directory and re-ran the command. Very likely this is not the answer to this solution, but searching my error brought me here, so it may help some others who are stuck.
I solved this issue by removing the symbolic link that was in /usr/local/bin and copying the actual python binary, that was pointed to by said link, there.
I had the same issue when I tried to use PyCharm. Solved by setting "python interpreter" in project configuration to point to the python virtual env I wanted to use, which was an Anaconda env. Somehow the interpreter path was missing the "anaconda" portion of ~/.../anaconda/.../_io.so. No need to uninstall anaconda.

Getting error while trying to create Python 3 virtual environment

I have Python 2.7 as default however I also have Python 3.5 installed separately
virtualenv -p /usr/local/bin/python3.5 kivyPy3.5
I am trying to setup Python 3.5 virtual env for kivy app development but I am getting the following error:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/enum/__init__.py", line 371, in __getattr__
return cls._member_map_[name]
KeyError: '_convert'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/virtualenv.py", line 23, in <module>
import subprocess
File "/usr/local/lib/python3.5/subprocess.py", line 364, in <module>
import signal
File "/usr/local/lib/python3.5/signal.py", line 8, in <module>
_IntEnum._convert(
File "/Library/Python/2.7/site-packages/enum/__init__.py", line 373, in __getattr__
raise AttributeError(name)
AttributeError: _convert
How can I resolve this error?
Installation:
step 1:
>> sudo easy_install virtualenv
step 2:
Creating the First Virtual Environment
>> mkdir first_evn
>> virtualenv first_env/test_env
or
>> virtualenv first_env/test_env --no-site-packages
-no-site-packages: If you don’t want to use any preinstalled packages from my operating system
step 3: activating environment
>> source /first_env/test_env/bin/activate
step 4: Deactivating Environment
>> deactivate
Important: if you have more than one versions of Python on your server or local system and you want to create a viertualenv for a specific version of python then please replace the step 2 with following
For Ubuntu
>> virtualenv --python=/usr/bin/python3.3 first_env/test_env
For Window
>> virtualenv --python=c:\Python33\python.exe first_env/test_env
For mac
virtualenv --python=python3.4 test_env
Adding virtual env path in .base_profile file
>>> pico ~/.bash_profile
And add live alias ff='source ~/PATH_FROM_ROOT/VIRTUAL_ENV_NAME/bin/activate'
This problem is caused by the version of the package enum34 that is installed in your system Python 2.7 interacting with the Python 3.5 you are attempting to use for the virtualenv.
To fix it, do the following using the instance of pip associated with your system Python 2.7.
pip install --upgrade enum34
Here's a Github issue conversation about it. https://github.com/pypa/virtualenv/issues/763

Unable to import git in python

I am facing these issues. Can you help me with the same ?
Why am I seeing this error ? Do I have to add anything in the requirements.txt file ?
>>> import git
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
import git
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
>>> from git import Repo
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
from git import Repo
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
I already had gitdb and smmap installed so I had to reinstall them.
You can reinstall them by running the following command in your terminal:
pip3 install --upgrade --force-reinstall gitdb; pip3 install --upgrade --force-reinstall smmap
I also got the message ImportError: 'gitdb' could not be found in your PYTHONPATH (when trying to use GitPython).BUT I had gitdb already installed!
Thanks to this hint I figured out that gitdb silently failed because it was missing smmap.
So I installed this and it worked.
You need to install gitdb package.
$ sudo easy_install gitdb
I had the same problem. However, gitdb and smmap were already installed by pip. As I used brew to install python and its dependencies on my mac, when I checked brew doctor command, it said that my /usr/local/sbin directory is not in my PATH. So I added it to my PATH (though it didn't have anything to do with the python) and everything worked out eventually.
MS Windows Versions of this problem can occur because of the order of Python versions in your system PATH, as it did for me. I did not realize that when I installed another program, it installed a newer version of Python for its own usage, and it appended my system PATH with the address to the newer version. I noticed it when I looked at the PATH variable and found two versions of Python being called. Windows uses the first it finds, and if the first doesn't match what your program expects, it gets confused and can't find the right path to the module. This is what I did to resolve it:
To check: an easy way to test if this is your problem is to see if the paths separated by semicolons are in the right order. That can be seen in the System Variables of Windows or by printing your PATH variable in your CMD shell like in this example:
C:> path
PATH=C:\Program Files (x86)\Python37-32\Scripts;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\WINDOWS
Temporary solution:
To see if it is going to fix your computer, change it in your CMD window. Your variable change will be discarded when the window is closed. One way to do this test is to copy the paths, move the Python references to the order they are needed, and write it back:
C:> set path = C:\WINDOWS;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\Program Files (x86)\Python37-32\Scripts\
Then run the Python program to see if this was your problem. Note that this is only an example; do not copy & paste it. Your path is customized for the programs on your computer.
Permanent solution: If the above test resolves your problem, you must change your System Variables to make the change permanent. For me that usually requires a reboot afterwards in order to make the variables appear in all new windows.

Error Installing imgseek on Mac Os

I am trying to install imgseek- the server version (http://www.imgseek.net/) to do image analysis.I am able to install all the dependencies successfully using:
sudo port install swig
sudo port install swig-python
sudo easy_install twisted
sudo port install imagemagick
sudo easy_install epydoc
Then I download isk-daemon from the downloads(isk-daemon-0.9.3.tar.gz) and build and install it. Everything runs succesfully.
But when I run iskdaemon.py from the command prompt, i get the following error:
sk-daemon : WARNING | no config file (isk-daemon.conf) found. Looked at local dir, home user dir and /etc/iskdaemon. Using defaults for everything.
root : ERROR Unable to load the C++ extension "_imgdb.so(pyd)" module.
root : ERROR See http://www.imgseek.net/isk-daemon/documents-1/compiling
Traceback (most recent call last):
File "/Users/gghanakota/anaconda/lib/python2.7/site-packages/isk_daemon-0.9.3-py2.7-macosx-10.5-x86_64.egg/imgSeekLib/ImageDB.py", line 35, in
import imgdb
File "/Users/gghanakota/anaconda/lib/python2.7/site-packages/isk_daemon-0.9.3-py2.7-macosx-10.5-x86_64.egg/imgSeekLib/imgdb.py", line 28, in
_imgdb = swig_import_helper()
File "/Users/gghanakota/anaconda/lib/python2.7/site-packages/isk_daemon-0.9.3-py2.7-macosx-10.5-x86_64.egg/imgSeekLib/imgdb.py", line 20, in swig_import_helper
import _imgdb
ImportError: dlopen(/Users/gghanakota/anaconda/lib/python2.7/site-packages/isk_daemon-0.9.3-py2.7-macosx-10.5-x86_64.egg/_imgdb.so, 2): Symbol not found: __ZNSs4_Rep20_S_empty_rep_storageE
Referenced from: /Users/gghanakota/anaconda/lib/python2.7/site-packages/isk_daemon-0.9.3-py2.7-macosx-10.5-x86_64.egg/_imgdb.so
Expected in: dynamic lookup
Please help!
I had the same problem when I was trying to install iskdaemon on my Mac (osx yosemite).
The problem in my case was that when I was building it, the c++ compiler threw two errors on using min function in imgdb.cpp
The error was because the types of the variables in the min function were not the same. Consequently the build failed and the imgdb module wasn't produced.
I fixed it by adding a simple typecast to the variables passed to the min function:
I changed: min(sz, numres) to min(sz, (long int)numres) on line 1003,
and min((V.size()/2), numres) to min((int)(V.size()/2), numres) on line 1327
I built again and it is working now.
Check if you get any errors when you build, maybe you are facing the same issue.

parallel installion of Python 2.7 and 3.3 via Homebrew - pip3 fails

I would like to make the jump and get acquainted with Python 3.
I followed the instructions found here with the installation working flawlessly.
I'm also able to use the provided virtualenv to create enviroments for Python 2 and Python 3 (Followed the instuctions here.). Unfortunalty pip3 fails when no virtualenv is activated. I need to use it to install global modules for python3.
This is the error message:
± |master ✓| → pip3
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/local/lib/python2.7/site-packages/distribute-0.6.45-py2.7.egg/pkg_resources.py", line 51
def _bypass_ensure_directory(name, mode=0777):
^
SyntaxError: invalid token
It looks like pip3 is trying to access distribute of python2. Is there any workaround for this?
I was having the same problem as you were and I had
export PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"
in my ~/.bash_profile. Removing that line solved the problem for me. If you have that or something like it in your ~/.bashrc or ~/.bash_profile, try removing it.

Categories

Resources