虚拟环境virtualenv问题PIP Install Numpy throws an error “ascii codec can't decode byte 0xe2”

转换虚拟环境:
virtualenv VIRTUALENV_DIR --system-site-packages

I have a freshly installed Ubuntu on a freshly built computer. I just installed python-pip using apt-get. Now when I try to pip install Numpy and Pandas, it gives the following error.

I've seen this error mentioned in quite a few places on SO and Google, but I haven't been able to find a solution. Some people mention it's a bug, some threads are just dead... What's going on?


 
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 185, in main
    return command.main(cmd_args)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 72: ordinal not in range(128)
share improve this question
 
3  
Are there non-ASCII characters in your hostname, home directory, &c.? Does setting LC_ALL=C make any difference? –  Charles Duffy  Oct 20 '14 at 20:15
1  
While this post is aimed at Amazon's EC2, it seems to be the same problem, and I find the answers more helpful: stackoverflow.com/questions/19595944/… –  BenjaminGolder  Dec 4 '14 at 12:07
 
I still do have a problem with the installation even though i have gotten numpy. Is there anyone else that have this problem? –  eleijonmarck  Mar 19 '15 at 21:45
 
As OP, and 3 years later, I can say I have solved this by migrating to Haskell ;) –  Josh.F  Aug 7 at 22:50

14 Answers

up vote 43 down vote accepted

I had this exact problem recently and used

 
apt-get install python-numpy

This adds numpy to your system python interpreter. I may have had to do the same for matplotlib. To use in a virtualenv, you have to create your environment using the

 
--system-site-packages

option

http://www.scipy.org/install.html

share improve this answer
 
1  
Thanks! Also, I found that if python-dev doesn't come stock on the computer, you need that too –  Josh.F  Oct 20 '14 at 20:16
 
Yeah. I remember that now.. Good catch. –  Jeff M.  Oct 20 '14 at 20:17
3  
You don't need to recreate your virtualenv, you can modify an existing one with virtualenv VIRTUALENV_DIR --system-site-packages. –  fiatjaf  Dec 6 '14 at 3:09
3  
Had same issue on Ubuntu server 14.02. sudo apt-get install python2.7-dev solved the issue. –  baltasvejas  May 25 '15 at 16:31
1  
This solves the problem but I think you should at least mention you are making (all) system packages available, so the point of using virtualenv is partially defeated... –  Mark  Aug 2 '15 at 13:53

For me @Charles Duffy comment solved it. Put this in your env:

LC_ALL=C

You can add it to your .bashrc with a line like this:

export LC_ALL=C

But take in care that you'll affect all other programs. So you may want to use it just for the pip run:

$ LC_ALL=C pip install ...

share improve this answer
 
1  
This seems to be the correct answer. Using --system-site-packages was not an option for me. –  moi  Aug 11 '15 at 13:16
4  
a better wording for you answer would be: add "export LC_ALL=C" to your ~/.bashrc –  Gil Hiram  Jun 28 '16 at 8:43
 
@GilHiram Depends on your shell type you may have to set up this env variable in other places. unix.stackexchange.com/questions/50665/… –  msemelman  Jun 29 '16 at 13:51
1  
The install didn't work for me both within, and outside a virtualenv so using --system-site-packages was not the right answer. I got it to work inside a virtualenv with LC_ALL=C pip install .... –  Arjun  Sep 19 '16 at 16:55
 
what does export LC_ALL=C mean / do ? –  javadba  Jun 27 at 20:40

Try updating pip:

 
pip install -U pip
share improve this answer
 
 
This worked for me on debian jessie inside of a venv. –  kalebo  May 4 at 15:47 

I had that problem with matplotlib package. I had to execute:

 
export LC_ALL=C
pip install --upgrade setuptools
share improve this answer
 

For me this was solved by ignoring a (presumably) corrupted cache with

 
pip install --no-cache-dir ...

as described here: https://github.com/pypa/pip/issues/2674

share improve this answer
 

A combination of

 
sudo apt-get install python-dev

and

 
export LC_ALL=C
pip install --upgrade setuptools

solved my problem.

share improve this answer
 

I had a similar error when running pip install pandas and it was due to a memory shortage. I increased the memory in my virtual machine to 4G and that fixed things.

share improve this answer
 
1  
Same here. Upgraded VM instance from 1 to a 2 gig RAM temporarily while installing. –  darwindave  Jan 15 '15 at 3:05

In 'site-packages' directory, make 'sitecustomize.py' like this

 
import sys
sys.setdefaultencoding("utf-8")

Now you can get the file 'pip.log'

share improve this answer
 
 
This is only an indirect answer, but it teaches something and does not deserve to be downvoted. –  user1158559  Mar 3 '16 at 17:05

try sudo apt-get install python-numpy . It worked out for me and same can be used for scipy,pandas etc by replacing them in place of numpy. (Y)

share improve this answer
 

If you want the pip version of numpy, you can build the dependencies for the package and then install it using pip

 
sudo apt-get build-dep python-numpy
pip install numpy

This should install everything needed at system level to install the package.

share improve this answer
 

Had a similar problem on a Jetson TK1 with Ubuntu.

Works fine with apt-get install python-pandas

share improve this answer
 

So many answers and none worked for me even though some clearly worked for other people. But I then figured out what my problem was, so I'll just add it to the collection:

 
dpkg-reconfigure locales
# enable the "en-US.UTF-8" locale
# when asked for a default, no need to define one

The thing is, I was working inside a Debian Stretch linux container that happened to not have any UTF-8 locales installed, probably because I downloaded a minimal stock image. With this UTF-8 locale now installed, pip properly installed numpy and other packages.

share improve this answer
 

In my case I had just installed Python from source (on a remote machine where I am not sudo). For whatever reason, pip was on some really old version. So after:

 
python -m pip install --upgrade pip

I was able to install numpy and everything I wanted without trouble.

share improve this answer
 

I met the similar problem. I tried:

 
export LC_ALL=C
pip install --upgrade setuptools

But it did not solve the problem, but another error came up:

AttributeError: 'str' object has no attribute 'rollback'

Then I tried:

 
pip install -U pip

Then the problem was solved.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个问题通常是由于安装的包不兼容当前Python版本导致的。解决方法如下: 1. 确认Python版本是否正确,可以在命令行输入python --version查看。 2. 确认安装的包是否支持当前Python版本,可以在官方文档中查看。 3. 如果安装的包不支持当前Python版本,可以尝试升级Python版本或者安装支持当前Python版本的包。 4. 如果以上方法都无法解决问题,可以尝试卸载当前Python版本并重新安装。 ### 回答2: 在使用pip安装软件包时,有时会遇到“SyntaxError: invalid syntax”的错误提示,这是由于Python版本不兼容导致的。在解决此问题之前,我们需要了解几个概念: 1. Python版本——在安装软件包之前,需要确认您的Python版本是否兼容该软件包。对于某些软件包,只能在特定的Python版本上运行。例如,某些开发工具包只能在Python 2.x版本上运行;其他软件包仅支持Python 3.x版本。 2. pip——pip是Python的包管理器,可以方便地安装、升级和卸载Python软件包。但是,pip不是Python的一部分。当您安装Python时,pip可能已经默认安装或需要手动安装。 3. 环境变量——在Windows环境中,所有Python相关的路径集中在环境变量PATH中。在Mac OS或Linux中,则使用默认位置/usr/local/bin/,不需要配置环境变量。 下面是一些解决“SyntaxError: invalid syntax”错误的方法: 1. 检查Python版本——确定您要安装的软件包是否与您正在运行的Python版本兼容。您可以在终端或命令提示符中键入python -V命令以查看您的Python版本。如果您需要更改Python版本,请根据您的操作系统和平台安装新的Python版本。 2. 更新pip——如果使用的pip版本过旧,则不支持一些新的软件包或功能。如果您有旧版本的pip,请使用以下命令升级:pip install -U pip。 3. 检查软件包——如果同时能在Python 2.x和Python 3.x上使用,则可能会出现与安装软件包有关的问题。检查软件包的文档或说明,以了解其支持的Python版本。或者,您可以在类Unix环境中使用Virtualenv或类似的工具来创建一个干净的Python环境,以避免与其他版本的Python发生冲突。 4. 检查环境变量——当您安装多个版本的Python时,可能需要配置环境变量,以便在终端或命令提示符中运行正确版本的Python。如果PATH环境变量中包含错误的Python路径,则可能会出现问题。检查您的路径是否正确设置,以确保正在使用正确的Python版本。 总之,如果您在使用pip安装软件包时遇到“SyntaxError: invalid syntax”错误,请先检查Python版本,更新pip版本,检查软件包和环境变量,以找到问题的根源。 ### 回答3: 当我们使用pip命令在Python环境下安装某一模块时,有时候可能会碰到“SyntaxError: invalid syntax”的报错提示。这通常是因为我们使用了错误的命令、参数或者不支持的Python版本等原因所致。 那么如何解决这个问题呢?以下是几种常见的解决方法: 1. 检查Python版本是否支持 我们可以通过pip命令的–version参数查看当前Python的版本。如果我们使用的是Python 2.x版本,则需要更新Python环境到Python 3.x版本,否则可能会碰到上述报错。我们可以使用以下命令将Python环境更新到最新的Python 3.x版本: $ sudo apt-get update $ sudo apt-get install python3 2. 使用正确的pip命令 我们需要确保使用的是正确的pip命令。如果我们使用的是Python 2.x版本,则需要使用pip2命令;如果我们使用的是Python 3.x版本,则需要使用pip3命令。如果我们在Python 3.x环境下尝试使用pip2命令,就会碰到“SyntaxError: invalid syntax”的报错。正确的pip命令应该是: $ pip3 install xxx 3. 检查输入命令是否正确 有时候我们输入的命令或参数存在错误,也会导致这个报错。比如我们在安装模块时,输入的模块名、命令格式或参数存在问题,就会出现“SyntaxError: invalid syntax”的报错。在这种情况下,我们需要仔细检查输入命令是否正确,尤其是空格、大小写等细节问题。 4. 切换虚拟环境 在使用虚拟环境时,我们需要先激活虚拟环境,然后再使用pip命令安装模块。如果我们在未激活虚拟环境下尝试安装模块,就会出现上述报错。因此,我们需要先切换到正确的虚拟环境下。 5. 更新pip版本 有时候我们的pip版本过旧,也会导致这个报错。我们可以通过以下命令更新pip版本: $ pip install --upgrade pip 以上就是解决“SyntaxError: invalid syntax”的一些方法。如果我们还是没有解决问题,可以尝试使用其它安装模块的方法,如使用source code编译安装等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值