virtualenv bug? - python

I am trying to run the following script in my virtualenv:
(python2.7.2-gonvaled1)gonvaled#lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ couchy.py
Traceback (most recent call last):
File "/home/gonvaled/projects/bin/couchy.py", line 46, in <module>
from couchdb_support import CouchdbLists
File "/home/gonvaled/projects/test_project/python_modules/couchdb_support.py", line 3615, in <module>
from asterisk_support import AsteriskSupport
File "/home/gonvaled/projects/test_project/python_modules/asterisk_support.py", line 4, in <module>
from starpy_support import AmiCommand
File "/home/gonvaled/projects/test_project/python_modules/starpy_support.py", line 7, in <module>
from starpy import manager, fastagi, utilapplication, menu
File "/usr/lib/python2.5/site-packages/starpy/utilapplication.py", line 2, in <module>
from basicproperty import common, propertied, basic, weak
File "/usr/lib/python2.5/site-packages/basicproperty-0.6.12a-py2.5-linux-i686.egg/basicproperty/common.py", line 159, in <module>
from basictypes import datedatetime_types as ddt
File "/usr/lib/python2.5/site-packages/basicproperty-0.6.12a-py2.5-linux-i686.egg/basictypes/datedatetime_types.py", line 4, in <module>
from dateutil import parser
File "/usr/lib/python2.5/site-packages/python_dateutil-2.1-py2.5.egg/dateutil/parser.py", line 8
from __future__ import unicode_literals
SyntaxError: future feature unicode_literals is not defined
So somehow python is trying to import from:
/usr/lib/python2.5/site-packages/starpy/utilapplication.py
This does not make any sense. My virtualenv is very clean:
(python2.7.2-gonvaled1)gonvaled#lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ python
Python 2.7.2 (default, Mar 29 2012, 00:31:29)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/Twisted-12.0.0-py2.7-linux-i686.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/zope.interface-3.8.0-py2.7-linux-i686.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/basicproperty-0.6.12a-py2.7-linux-i686.egg', '/home/gonvaled/projects/gonvaled_settings', '/home/gonvaled/django_apps', '/home/gonvaled/projects/test_project/ipc', '/home/gonvaled/projects/test_project/python_modules', '/home/gonvaled/projects/test_project/gdata', '/home/gonvaled/projects/callisto/libraries', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python27.zip', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/plat-linux2', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-tk', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-old', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-dynload', '/usr/local/python/2.7.2/lib/python2.7', '/usr/local/python/2.7.2/lib/python2.7/plat-linux2', '/usr/local/python/2.7.2/lib/python2.7/lib-tk', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages']
And:
(python2.7.2-gonvaled1)gonvaled#lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ pip freeze
CouchDB==0.9dev
DateUtils==0.5.1
Twisted==12.0.0
basicproperty==0.6.12a
pystache==0.4.0
python-dateutil==2.1
pytz==2012b
simplejson==2.4.0
six==1.1.0
starpy==1.0.0a12
wsgiref==0.1.2
zope.interface==3.8.0
So to summarize: python is importing from /usr/lib/python2.5/site-packages even though I am inside a virtualenv. How can this be?

Try running the script as:
$ python couch.py
in your activated virtualenv. Depending on your system and the first line (the shebang line) of the file, you may not be running in the Python you think you are.

You should create your virtualenv with the --nositepackages option to keep it clean and away from your system Python.

Related

Unable to import statsmodels

I am new to python and I am trying to learn how to use it for statistics. I have been trying to use the
from statsmodels.stats.weightstats import ztest
But I get an error when I try to use this command.
Here is what it displayed:
Python 2.7.17 (default, Nov 7 2019, 10:07:09)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from statsmodels.stats.weightstats import ztest
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/stats/__init__.py", line 1, in <module>
from statsmodels.tools._testing import PytestTester
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/tools/__init__.py", line 1, in <module>
from .tools import add_constant, categorical
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/tools/tools.py", line 8, in <module>
from statsmodels.compat.python import lzip, lmap
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/compat/__init__.py", line 1, in <module>
from statsmodels.tools._testing import PytestTester
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/tools/_testing.py", line 11, in <module>
from statsmodels.compat.pandas import assert_equal
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/compat/pandas.py", line 4, in <module>
import numpy as np
File "/home/jico/.local/lib/python2.7/site-packages/statsmodels/compat/numpy.py", line 46, in <module>
NP_LT_114 = LooseVersion(np.__version__) < LooseVersion('1.14')
AttributeError: 'module' object has no attribute '__version__'
>>>
Any help that I can get would be appreciated. I am using python 2.7.17
I'm also working on python 2.7 facing exactly the same problem.
Here's how I solved it:
First thing first, it says in error message
NP_LT_114 = LooseVersion(np.version) < LooseVersion('1.14')
Thus make sure you have numpy version > 1.14
Secondly, I downgraded statsmodels from 0.11.0 to 0.10.0.
Hope it works for you too.
(numpy==1.16.5, statsmodels==0.10.0, python=2.7)
Just had this issue after conda chose to update numpy from 1.13.3 to 1.14.3. Reverting numpy via procedure in http://blog.rtwilson.com/conda-revisions-letting-you-rollback-to-a-previous-version-of-your-environment/ sufficed to recover functionality, followed by pinning the version in the environment.
also wondering what does shows:
import numpy; print(numpy.__file__)

ModuleNotFoundError: No module named '_socket' after installing python3 to a local folder

I installed Python 3.7.4 from sources to a local folder. Installation did not produce any errors, but the binaries do not work.
That's what I did to install and setup the software
./configure --prefix=/usr/local/python3.7.4
make -j 8 install
export PYTHONHOME="/usr/local/python3.7.4/"
export PYTHONPATH="/usr/local/python3.7.4/lib/python3.7/site-packages/"
That's what I get when attempt to run python3
/usr/local/python3.7.4/bin/python3
Python 3.7.4 (default, Sep 4 2019, 19:14:06)
[GCC 4.8.3 20140627 [gcc-4_8-branch revision 212064]] on linux
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/etc/pythonstart", line 7, in <module>
import readline
ModuleNotFoundError: No module named 'readline'
and this is what I get from pip3:
/usr/local/python3.7.4/bin/pip3
Traceback (most recent call last):
File "/usr/local/python3.7.4/bin/pip3", line 6, in <module>
from pip._internal import main
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_internal/__init__.py", line 19, in <module>
from pip._vendor.urllib3.exceptions import DependencyWarning
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_vendor/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/usr/local/python3.7.4/lib/python3.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 7, in <module>
from socket import error as SocketError, timeout as SocketTimeout
File "/usr/local/python3.7.4/lib/python3.7/socket.py", line 49, in <module>
import _socket
ModuleNotFoundError: No module named '_socket'
I feel that the core of the problem is the local installation.
I am sorry if my question is trivial. I tried googling it with no success.
Thank you for your time.
The problem was resolved by adding more paths to the PYTHONPATH variable
export PYTHONPATH="/usr/local/python3.7.4/lib/python3.7/site-packages/:/usr/local/python3.7.4/:/usr/local/python3.7.4/lib64/python3.7/lib-dynload/"

Managed to break my venv; is it possible to fix?

I managed to break one of my virtual environments. I think I did it by installing pycrypto with pip and uninstalling it with easy_install.
This is what I get when I run python in the venv.
filip#dratmac:~/PycharmProjects/Project-Marketplace2/backend/odb/helper_objects$ python
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 62, in <module>
import os
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 49, in <module>
import posixpath as path
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 17, in <module>
import warnings
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 8, in <module>
import types
File "types.py", line 1, in <module>
import json
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 108, in <module>
from .decoder import JSONDecoder
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 3, in <module>
import re
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 280, in <module>
import copy_reg
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 7, in <module>
from types import ClassType as _ClassType
ImportError: cannot import name ClassType
filip#dratmac:~/PycharmProjects/Project-Marketplace2/backend/odb/helper_objects$
And outside the venv everything works as expected:
Last login: Tue Apr 7 22:30:08 on ttys005
filip#dratmac:~$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
How can I fix this?
You should not name your file types.py as there is a standard library module with the same name (import types).
As such, Python tries to import it as part of some standard library code but it now actually imports your local types.py file, not the module it expects.
So the problem should be fixed when you rename types.py to something else.

keep getting 'ImportError: No module named pygtk' when I try to run Gaphor on windows

I have installed pygtk-all-in-one-2.24.0.win32-py2.7.msi on windows, and installed gaphor by easy_install. When I try to run gaphor, I receive an import error.
PS C:\Python27\Scripts> .\gaphor.exe
Traceback (most recent call last):
File "C:\Python27\Scripts\gaphor-script.py", line 9, in <module>
load_entry_point('gaphor==0.17.2', 'console_scripts', 'gaphor')()
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 521, in load_entry_point
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2632, in load_entry_point
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2312, in load
File "build\bdist.win32\egg\pkg_resources\__init__.py", line 2318, in resolve
File "C:\Python27\lib\site-packages\gaphor-0.17.2-py2.7.egg\gaphor\__init__.py", line 13, in <module>
import pygtk
ImportError: No module named pygtk
Any suggestions would be appreciated!
Did you actually install pygtk? I suspect easy-install should have pulled it in automatically. Did you see any errors during the install? pygtk also requieres installing gtk+.
Do a simple test - start Python in a console (ipython?), and type:
$ python
Python 2.7.6 (default, May 6 2014, 10:57:58)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> import gtk
>>>
There should be no error messages.
You can also search the disk for pygtk.py (with the file manager) and for a directory gtk-2.0 or gtk-3.0.

Python 2.6 complains about missing a math module - I cannot use it after some (other) software update. Any tips?

I am relatively new with Python. Here is my problem:
My environment was python2.6 with MacOs 10.6.8. I recently had to update my system with Python2.7 and some other stuff (Eclipse 3.6 and GoogleAppEngine). Python works fine within Eclipse, however, I want to keep the shell-mode in being able to quickly test small stuff on the commandline. I am getting this error message, and I need this to get rid of asap:
$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py",
line 92, in <module>
import httplib
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py",
line 77, in <module>
import mimetools
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/mimetools.py",
line 6, in <module>
import tempfile
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/tempfile.py",
line 34, in <module>
from random import Random as _Random
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/random.py",
line 45, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: dlopen(/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/lib-dynload/math.so, 2): Symbol not found: __PyLong_AsScaledDouble
Referenced from: /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/lib-dynload/math.so
Expected in: flat namespace
in /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/math.so
Anyway, this was the python version that my macbook was shipped with at buying time. I don't think i want to waste time on removing, re-stalling etc. an apple-approved python version. Does anybody know how I can get my system back to run properly? And why does this error occur at all? Thanks for quick help!
Try to run your new python within a virtualenv, so that it is isolated from the system install.
http://pypi.python.org/pypi/virtualenv

Categories

Resources