标签归档:macos

Mac OS X-EnvironmentError:找不到mysql_config

问题:Mac OS X-EnvironmentError:找不到mysql_config

首先,是的,我已经看到了:

pip安装mysql-python失败,并显示EnvironmentError:找不到mysql_config

问题

我正在尝试在Google App Engine项目上使用Django。但是,由于以下原因,服务器无法正常启动,因此无法启动。

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我做了一些研究,所有这些都指出必须安装Mysql-python,因为它显然不在我的系统上。我实际上尝试卸载它,并得到了:

Cannot uninstall requirement mysql-python, not installed

每当我真正尝试通过以下方式安装时:

sudo pip install MySQL-python

我收到一条错误消息:

raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

我已经尝试运行:

export PATH=$PATH:/usr/local/mysql/bin

但这似乎没有帮助,因为我再次运行了安装命令,但仍然失败。

有任何想法吗?

请注意,我不在virtualenv中。

First off, yeah, I’ve already seen this:

pip install mysql-python fails with EnvironmentError: mysql_config not found

The problem

I am trying to use Django on a Google App Engine project. However, I haven’t been able to get started as the server fails to start properly due to:

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I did some research and it all pointed to having to install Mysql-python, as apparently it isn’t on my system. I actually tried uninstalling it and got this:

Cannot uninstall requirement mysql-python, not installed

Whenever I actually do try to install via:

sudo pip install MySQL-python

I get an error stating:

raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

I’ve already tried running:

export PATH=$PATH:/usr/local/mysql/bin

but that didn’t seem to help, as I ran the installation command again and it still failed.

Any ideas?

Please note I’m not in a virtualenv.


回答 0

好吧,首先,让我检查一下我是否和您在同一页面上:

  • 您安装了python
  • 你做了 brew install mysql
  • 你做了 export PATH=$PATH:/usr/local/mysql/bin
  • 最后,您做到了pip install MySQL-Python(或者pip3 install mysqlclient如果使用python 3)

如果您以相同的顺序执行了所有这些步骤,但是仍然出现错误,请继续阅读,如果您没有按照这些确切的步骤尝试,那么请从头开始。

因此,您按照这些步骤进行操作,仍然遇到错误,可以尝试以下操作:

  1. 尝试which mysql_config从bash 运行。可能找不到。这就是为什么构建也找不到它的原因。尝试运行locate mysql_config,看看是否有任何返回。该二进制文件的路径需要在您外壳的$ PATH环境变量中,或者必须在该模块的setup.py文件中显式地存在(假设它正在某个特定位置查找该文件)。

  2. 除了使用MySQL-Python,还可以尝试使用“ mysql-connector-python”,它可以使用安装pip install mysql-connector-python。有关此的更多信息,请参见此处此处

  3. 手动找到“ mysql / bin”,“ mysql_config”和“ MySQL-Python”的位置,并将所有这些都添加到$ PATH环境变量中。

  4. 如果以上所有步骤均失败,则可以尝试使用MacPorts安装“ mysql”,在这种情况下,文件“ mysql_config”实际上将被称为“ mysql_config5”,在这种情况下,必须在安装后执行以下操作:export PATH=$PATH:/opt/local/lib/mysql5/bin。您可以在此处找到更多详细信息。

注意1:我见过有人说安装python-dev并提供了libmysqlclient-dev帮助,但是我不知道这些软件包是否在Mac OS上可用。

注意2:此外,请确保尝试以root用户身份运行命令。

我得到了我的答案,从(除了我的大脑)这些地方(也许你可以看看他们,看看它是否会有所帮助):1234

我希望我能提供帮助,并且很高兴知道其中任何一个是否有效。祝好运。

Ok, well, first of all, let me check if I am on the same page as you:

  • You installed python
  • You did brew install mysql
  • You did export PATH=$PATH:/usr/local/mysql/bin
  • And finally, you did pip install MySQL-Python (or pip3 install mysqlclient if using python 3)

If you did all those steps in the same order, and you still got an error, read on to the end, if, however, you did not follow these exact steps try, following them from the very beginning.

So, you followed the steps, and you’re still geting an error, well, there are a few things you could try:

  1. Try running which mysql_config from bash. It probably won’t be found. That’s why the build isn’t finding it either. Try running locate mysql_config and see if anything comes back. The path to this binary needs to be either in your shell’s $PATH environment variable, or it needs to be explicitly in the setup.py file for the module assuming it’s looking in some specific place for that file.

  2. Instead of using MySQL-Python, try using ‘mysql-connector-python’, it can be installed using pip install mysql-connector-python. More information on this can be found here and here.

  3. Manually find the location of ‘mysql/bin’, ‘mysql_config’, and ‘MySQL-Python’, and add all these to the $PATH environment variable.

  4. If all above steps fail, then you could try installing ‘mysql’ using MacPorts, in which case the file ‘mysql_config’ would actually be called ‘mysql_config5’, and in this case, you would have to do this after installing: export PATH=$PATH:/opt/local/lib/mysql5/bin. You can find more details here.

Note1: I’ve seen some people saying that installing python-dev and libmysqlclient-dev also helped, however I do not know if these packages are available on Mac OS.

Note2: Also, make sure to try running the commands as root.

I got my answers from (besides my brain) these places (maybe you could have a look at them, to see if it would help): 1, 2, 3, 4.

I hoped I helped, and would be happy to know if any of this worked, or not. Good luck.


回答 1

我一直在调试此问题-3小时17分钟。令我特别烦恼的是,我已经通过先前的uni工作在系统上安装了sql,但是pip / pip3却无法识别它。这些线程以及我在互联网上搜索过的许多其他线程有助于消除问题,但实际上并没有解决问题。

回答

Pip在相对于Macintosh HD @ 的Homebrew目录中寻找mysql二进制文件

/usr/local/Cellar/

所以我发现这需要您进行一些更改

步骤1:如果尚未下载MySql,请下载https://dev.mysql.com/downloads/

步骤2:相对于Macintosh HD和cd 找到它

/usr/local/mysql/bin

第3步:打开终端并使用所选的文本编辑器后-我本人是个Neovim家伙,所以我打了一下(Mac并不会自动附带……另一天的故事)

nvim mysql_config

第4步:您将在第112行看到

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

改成

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

*您会注意到该文件具有只读访问权限,因此,如果您使用vim或neovim

:w !sudo tee %

步骤5:转到主目录并编辑.bash_profile文件

cd ~

然后

nvim .bash_profile

并添加

export PATH="/usr/local/mysql/bin:$PATH"

到文件然后保存

步骤6:相对于Macintosh HD找到路径并将其添加

cd /private/etc/

然后

nvim paths

并添加

/usr/local/mysql/bin

*您会再次注意到该文件具有只读访问权限,因此,如果您使用vim或neovim

:w !sudo tee % 

然后

cd ~

然后通过运行您的更改刷新终端

source .bash_profile

最后

pip3 install mysqlclient

还有中提琴 记住这是一种氛围。

I had been debugging this problem forever – 3 hours 17 mins. What particularly annoyed me was that I already had sql installed on my system through prior uni work but pip/pip3 wasn’t recognising it. These threads above and many other I scoured the internet for were helpful in eluminating the problem but didn’t actually solve things.

ANSWER

Pip is looking for mysql binaries in the Homebrew Directory which is located relative to Macintosh HD @

/usr/local/Cellar/

so I found that this requires you making a few changes

step 1: Download MySql if not already done so https://dev.mysql.com/downloads/

Step 2: Locate it relative to Macintosh HD and cd

/usr/local/mysql/bin

Step 3: Once there open terminal and use a text editor of choice – I’m a neovim guy myself so I typed (doesn’t automatically come with Mac… another story for another day)

nvim mysql_config

Step 4: You will see at approx line 112

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

Change to

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

*you’ll notice that this file has read-only access so if your using vim or neovim

:w !sudo tee %

Step 5: Head to the home directory and edit the .bash_profile file

cd ~

Then

nvim .bash_profile

and add

export PATH="/usr/local/mysql/bin:$PATH"

to the file then save

Step 6: relative to Macintosh HD locate paths and add to it

cd /private/etc/

then

nvim paths

and add

/usr/local/mysql/bin

*you’ll again notice that this file has read-only access so if your using vim or neovim

:w !sudo tee % 

then

cd ~

then refresh the terminal with your changes by running

source .bash_profile

Finally

pip3 install mysqlclient

And Viola. Remember it’s a vibe.


回答 2

如果您不想安装完整的mysql,我们可以通过安装mysqlclient来解决此问题。cmd brew install mysqlclient 完成后,将要求在下面添加以下行~/.bash_profile

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

关闭终端并启动新终端,然后继续 pip install mysqlclient

If you don’t want to install full mysql, we can fix this by just installing mysqlclient brew install mysqlclient Once cmd is completed it will ask to add below line to ~/.bash_profile:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

Close terminal and start new terminal and proceed with pip install mysqlclient


回答 3

我在MacOS Catalina上运行Python 3.6。我的问题是我尝试安装mysqlclient==1.4.2.post1,并不断抛出mysql_config not found错误。

这是我解决问题的步骤。

  1. 使用brew安装mysql-connector-c(如果已经有mysql,请先安装unlink brew unlink mysql)-brew install mysql-connector-c
  2. 打开mysql_config并编辑第112行附近的文件
# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl -这将为您提供更多有关将openssl放入PATH中需要执行的操作的信息
  2. 关于步骤3,您需要执行以下操作将openssl放入PATH中- echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
  3. 供编译器找到openssl- export LDFLAGS="-L/usr/local/opt/openssl/lib"
  4. 供编译器找到openssl- export CPPFLAGS="-I/usr/local/opt/openssl/include"

I am running Python 3.6 on MacOS Catalina. My issue was that I tried to install mysqlclient==1.4.2.post1 and it keeps throwing mysql_config not found error.

This is the steps I took to solve the issue.

  1. Install mysql-connector-c using brew (if you have mysql already install unlink first brew unlink mysql) – brew install mysql-connector-c
  2. Open mysql_config and edit the file around line 112
# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl – this will give you more information on what needs to be done about putting openssl in PATH
  2. in relation to step 3, you need to do this to put openssl in PATH – echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
  3. for compilers to find openssl – export LDFLAGS="-L/usr/local/opt/openssl/lib"
  4. for compilers to find openssl – export CPPFLAGS="-I/usr/local/opt/openssl/include"

回答 4

当我安装mysqlclient时也会发生这种情况,

$ pip install mysqlclient

如用户3429036所说,

$ brew install mysql

Also this happens when I was installing mysqlclient,

$ pip install mysqlclient

As user3429036 said,

$ brew install mysql

回答 5

此答案适用于brew不是从官方安装而是从官方安装的MacOS用户.dmg/.pkg。该安装程序无法编辑您的PATH,从而导致出现问题:

  1. 所有MySQL命令一样mysqlmysqladminmysql_config,等不能被发现,并作为一个结果:
  2. “MySQL的预置面板”未出现在系统偏好设置,
  3. 您不能安装任何与MySQL通信的API,包括 mysqlclient

您要做的是附加MySQL bin文件夹(通常/usr/local/mysql/bin在您PATH~/.bash_profile文件中添加以下行:

export PATH="/usr/local/mysql/bin/:$PATH"

然后,您应该重新加载您~/.bash_profile的更改,以使更改在当前的终端会话中生效:

source ~/.bash_profile

mysqlclient但是,在安装之前,您需要接受XcodeBuild许可证:

sudo xcodebuild -license

按照他们的指示退出您的家人,之后您应该可以顺利安装mysqlclient

pip install mysqlclient

安装在此之后,你必须做一件事来解决运行时错误,船舶与MySQL(动态库libmysqlclient.dylib未找到),加入这行到你的系统动态库路径:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH

This answer is for MacOS users who did not install from brew but rather from the official .dmg/.pkg. That installer fails to edit your PATH, causing things to break out of the box:

  1. All MySQL commands like mysql, mysqladmin, mysql_config, etc cannot be found, and as a result:
  2. the “MySQL Preference Pane” fails to appear in System Preferences, and
  3. you cannot install any API that communicates with MySQL, including mysqlclient

What you have to do is appending the MySQL bin folder (typically /usr/local/mysql/bin in your PATH by adding this line in your ~/.bash_profile file:

export PATH="/usr/local/mysql/bin/:$PATH"

You should then reload your ~/.bash_profile for the change to take effect in your current Terminal session:

source ~/.bash_profile

Before installing mysqlclient, however, you need to accept the XcodeBuild license:

sudo xcodebuild -license

Follow their directions to sign away your family, after which you should be able to install mysqlclient without issue:

pip install mysqlclient

After installing that, you must do one more thing to fix a runtime bug that ships with MySQL (Dynamic Library libmysqlclient.dylib not found), by adding this line to your system dynamic libraries path:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH


回答 6

如果您通过指定版本使用Homebrew安装了mysql ,则mysql_config将出现在此处。– /usr/local/Cellar/mysql@5.6/5.6.47/bin

您可以在/ usr / local /目录中使用ls命令找到sql bin的路径

/usr/local/Cellar/mysql@5.6/5.6.47/bin

像这样将路径添加到bash配置文件。

nano ~/.bash_profile

export PATH="/usr/local/Cellar/mysql@5.6/5.6.47/bin:$PATH"

If you have installed mysql using Homebrew by specifying a version then mysql_config would be present here. – /usr/local/Cellar/mysql@5.6/5.6.47/bin

you can find the path of the sql bin by using ls command in /usr/local/ directory

/usr/local/Cellar/mysql@5.6/5.6.47/bin

Add the path to bash profile like this.

nano ~/.bash_profile

export PATH="/usr/local/Cellar/mysql@5.6/5.6.47/bin:$PATH"

回答 7

在我的情况下,问题是我在python虚拟环境中运行命令,尽管我将其放在.bash_profile文件中,但它没有/ usr / local / mysql / bin的路径。只是在虚拟环境中导出路径对我有用。

对于您的信息,sql_config位于bin目录中。

The problem in my case was that I was running the command inside a python virtual environment and it didn’t had the path to /usr/local/mysql/bin though I have put it in the .bash_profile file. Just exporting the path in the virtual env worked for me.

For your info sql_config resides inside bin directory.


回答 8

对我来说,安装brew或apt-get也不容易,所以我通过以下网址下载了mysql:https//dev.mysql.com/downloads/connector/python/,并安装了它。因此,我可以在以下目录中找到mysql_config:/ usr / local / mysql / bin

下一步是:

  1. 导出PATH = $ PATH:/ usr / local / mysql / bin
  2. 点安装MySQL-python == 1.2.5

Install brew or apt-get is also not easy for me so I downloaded mysql via: https://dev.mysql.com/downloads/connector/python/, installed it. So I can find mysql_config int this directory: /usr/local/mysql/bin

the next step is:

  1. export PATH=$PATH:/usr/local/mysql/bin
  2. pip install MySQL-python==1.2.5

pip或pip3为Python 3安装软件包?

问题:pip或pip3为Python 3安装软件包?

我有一台配备OS X El Captain的Macbook。我认为那Python 2.7是预装的。但是,我也安装Python 3.5了。开始使用时Python 3,我读到要安装软件包,应输入:

pip3 install some_package

无论如何,现在当我使用

pip install some_package

some_package安装了Python 3。我的意思是我可以导入它,并且可以毫无问题地使用它。而且,当我只pip3输入时Terminal,我得到以下关于用法的消息:

Usage:   
  pip <command> [options]

这是我输入just时得到的相同信息pip

这是否意味着在previos版本,事情是不同的,现在pippip3可以互换使用?如果是这样,并且为了参数起见,我该如何安装软件包Python 2而不是Python 3

I have a Macbook with OS X El Captain. I think that Python 2.7 comes preinstalled on it. However, I installed Python 3.5 too. When I started using Python 3, I read that if I want to install a package, I should type:

pip3 install some_package

Anyway, now when I use

pip install some_package

I get some_package installed for Python 3. I mean I can import it and use it without problems. Moreover, when I type just pip3 in Terminal, I got this message about the usage:

Usage:   
  pip <command> [options]

which is the same message I get when I type just pip.

Does it mean that in previos versions, things were different, and now pip and pip3 can be used interchangeably? If so, and for the sake of argument, how can I install packages for Python 2 instead of Python 3?


回答 0

pip是使用链接到同一可执行文件路径的软链接pip3。您可以使用以下命令来检查您的真实路径pippip3实际路径:

$ ls -l `which pip`
$ ls -l `which pip3`

您也可以使用以下命令了解更多详细信息:

$ pip show pip
$ pip3 show pip

当我们安装不同版本的python时,我们可能会创建以下软链接到

  • 将默认点设置为某些版本。
  • 为不同的版本创建不同的链接。

这是同样的情况pythonpython2python3

如果您对在不同情况下如何发生感兴趣,可以在下面获取更多信息:

Your pip is a soft link to the same executable file path with pip3. you can use the commands below to check where your pip and pip3 real paths are:

$ ls -l `which pip`
$ ls -l `which pip3`

You may also use the commands below to know more details:

$ pip show pip
$ pip3 show pip

When we install different versions of python, we may create such soft links to

  • set default pip to some version.
  • make different links for different versions.

It is the same situation with python, python2, python3

More information below if you’re interested in how it happens in different cases:


回答 1

如果您安装了python 2.x,然后安装了python3,则您的pip将指向pip3。您可以通过键入来验证pip --version与相同pip3 --version

在您的系统上,现在有了pip,pip2和pip3

如果需要,可以将pip更改为指向pip2而不是pip3。

If you had python 2.x and then installed python3, your pip will be pointing to pip3. you can verify that by typing pip --version which would be the same as pip3 --version.

On your system you have now pip, pip2 and pip3.

If you want you can change pip to point to pip2 instead of pip3.


回答 2

安装后python3,即pip3会安装。如果你没有其他的Python安装(如python2.7),然后链接创建这点pippip3

所以pip是一个链接pip3,如果有Python安装的任何其他版本(比python3等)。 pip通常指向首次安装。

When you install python3, pip3 gets installed. And if you don’t have another python installation(like python2.7) then a link is created which points pip to pip3.

So pip is a link to to pip3 if there is no other version of python installed(other than python3). pip generally points to the first installation.


回答 3

这是一个棘手的问题。最后,如果您调用pip它,则将调用pip2pip3,具体取决于您如何设置系统。

This is a tricky subject. In the end, if you invoke pip it will invoke either pip2 or pip3, depending on how you set your system up.


回答 4

我认为pippip2并且pip3不是指向同一可执行文件路径的软链接。请注意以下命令并在我的linux终端中显示结果:

mrz@mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10  2016 /usr/bin/pip
mrz@mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10  2016 /usr/bin/pip2
mrz@mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10  2016 /usr/bin/pip3
mrz@mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz@mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz@mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)

如您所见,它们存在于不同的路径中。

pip3始终仅在Python3环境上运行,就像pip2在Python2上一样。pip在适合上下文的任何环境下运行。例如,如果您在Python3平台上,pip将在Python3环境下运行。

I think pip, pip2 and pip3 are not soft links to the same executable file path. Note these commands and results in my Linux terminal:

mrz@mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10  2016 /usr/bin/pip
mrz@mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10  2016 /usr/bin/pip2
mrz@mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10  2016 /usr/bin/pip3
mrz@mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz@mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz@mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)

As you see they exist in different paths.

pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates in whichever environment is appropriate to the context. For example, if you are in a Python3 venv, pip will operate on the Python3 environment.


回答 5

通过插图:

pip --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

pip3 --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

python --version
  Python 3.7.3

which python
  /usr/bin/python

ls -l '/usr/bin/python'
  lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3

which python3
  /usr/bin/python3

ls -l /usr/bin/python3
  lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7

ls -l /usr/bin/python3.7
  -rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7

因此,在我的默认系统python(Python 3.7.3)中,my pip pip3

By illustration:

pip --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

pip3 --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

python --version
  Python 3.7.3

which python
  /usr/bin/python

ls -l '/usr/bin/python'
  lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3

which python3
  /usr/bin/python3

ls -l /usr/bin/python3
  lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7

ls -l /usr/bin/python3.7
  -rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7

Thus, my in my default system python (Python 3.7.3), pip is pip3.


回答 6

如果您安装了Python 2.7,我想您可以使用pip2pip2.7安装专门针对Python 2的软件包,例如

pip2 install some_pacakge

要么

pip2.7 install some_package

您可以使用pip3pip3.5安装专门用于Python 3的pacakges。

If you installed Python 2.7, I think you could use pip2 and pip2.7 to install packages specifically for Python 2, like

pip2 install some_pacakge

or

pip2.7 install some_package

And you may use pip3 or pip3.5 to install pacakges specifically for Python 3.


回答 7

在我的Windows实例上-并且我不完全了解我的环境-使用pip3安装kaggle-cli程序包有效-而pip无效。我在conda环境中工作,环境似乎有所不同。

(fastai)C:\ Users \ redact \ Downloads \ fast.ai \ deeplearning1 \ nbs> pip –version

来自C:\ ProgramData \ Anaconda3 \ envs \ fastai \ lib \ site-packages的pip 9.0.1(python 3.6)

(fastai)C:\ Users \ redact \ Downloads \ fast.ai \ deeplearning1 \ nbs> pip3 –version

来自c:\ users \ redact \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages(python 3.6)的pip 9.0.1

On my Windows instance – and I do not fully understand my environment – using pip3 to install the kaggle-cli package worked – whereas pip did not. I was working in a conda environment and the environments appear to be different.

(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip –version

pip 9.0.1 from C:\ProgramData\Anaconda3\envs\fastai\lib\site-packages (python 3.6)

(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip3 –version

pip 9.0.1 from c:\users\redact\appdata\local\programs\python\python36\lib\site-packages (python 3.6)


回答 8

somepath / venv中激活了Python 3.6 virtualenv后,以下别名解决了macOS Sierra上的各种问题,其中pip坚持指向Apple的2.7 Python。

alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

当我不得不做的时候,这不是很好,sudo pip因为root用户对我的别名或virtualenv一无所知,所以我也不得不添加一个额外的别名来处理这个问题。这是一个hack,但是可以用,我知道它的作用:

alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

背景:

pip3不存在,无法启动(找不到命令),which pip它将返回Apple Python的/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip

Python 3.6是通过macports安装的。

激活我想使用的3.6 virtualenv后,which python将返回somepath / venv / bin / python

以某种方式pip install可以做正确的事情并击中我的virtualenv,但pip list会使Python 2.7包感到不安。

对于Python而言,这对初学者友好程度比我期望的要低。

Given an activated Python 3.6 virtualenv in somepath/venv, the following aliases resolved the various issues on a macOS Sierra where pip insisted on pointing to Apple’s 2.7 Python.

alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

This didn’t work so well when I had to do sudo pip as the root user doesn’t know anything about my alias or the virtualenv, so I had to add an extra alias to handle this as well. It’s a hack, but it works, and I know what it does:

alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

background:

pip3 did not exist to start (command not found) with and which pip would return /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip, the Apple Python.

Python 3.6 was installed via macports.

After activation of the 3.6 virtualenv I wanted to work with, which python would return somepath/venv/bin/python

Somehow pip install would do the right thing and hit my virtualenv, but pip list would rattle off Python 2.7 packages.

For Python, this is batting way beneath my expectations in terms of beginner-friendliness.


在sudo下运行pip install是否可以接受并且安全?

问题:在sudo下运行pip install是否可以接受并且安全?

我已经开始使用Mac来安装Python软件包,就像在工作中使用Windows PC一样。但是,在Mac上,我在写入日志文件或站点程序包时经常遇到权限被拒绝的错误。

因此,我考虑过pip install <package>sudosudo 下运行,但是考虑到我只是想将其安装在当前用户帐户下,是否安全/可接受地使用sudo?

日志文件I / O错误的示例回溯:

Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto
Storing complete log in /Users/markwalker/Library/Logs/pip.log
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 8, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
    return command.main(args[1:], options)
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
    log_fp = open_logfile(log_fn, 'w')
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
    log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

更新 这可能取决于权限,但是最好的方法是为您的python项目使用虚拟环境。sudo pip除非绝对必要,否则应避免运行。

I’ve started to use my Mac to install Python packages in the same way I do with my Windows PC at work; however on my Mac I’ve come across frequent permission denied errors while writing to log files or site-packages.

Therefore I thought about running pip install <package> under sudo but is that a safe/acceptable use of sudo considering I’m just wanting this to be installed under my current user account?

Example traceback from a logfile I/O error:

Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto
Storing complete log in /Users/markwalker/Library/Logs/pip.log
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 8, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
    return command.main(args[1:], options)
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
    log_fp = open_logfile(log_fn, 'w')
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
    log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

Update This was likely down to permissions, however the best approach is to use virtual environments for your python projects. Running sudo pip should be avoided unless absolutely necessary.


回答 0

使用虚拟环境

$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want

sudo当您要为全局的系统级Python安装安装内容时,才使用或提升权限。

最好使用虚拟环境为您隔离软件包。这样一来,您就可以畅玩而不会污染全局python安装。

另外,virtualenv不需要提升的权限。

Use a virtual environment:

$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want

You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation.

It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.

As a bonus, virtualenv does not need elevated permissions.


回答 1

它是可接受的安全运行pip installsudo

它不安全并且被皱着眉头–请参阅运行“ sudo pip”有什么风险? 要在主目录中安装Python软件包,您不需要root特权。见描述--user选项点子。

Is it acceptable & safe to run pip install under sudo?

It’s not safe and it’s being frowned upon – see What are the risks of running ‘sudo pip’? To install Python package in your home directory you don’t need root privileges. See description of --user option to pip.


回答 2

您最初的问题是pip无法将日志写入文件夹。

IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

您需要将cd放入一个文件夹,在该文件夹中,调用的进程可以像/tmp这样写,cd /tmp然后重新调用该命令可能会起作用,但这不是您想要的。

实际上对于这种特殊情况(您不希望sudo用于安装python软件包)并且不需要全局软件包安装,可以使用如下--user标记:

pip install --user <packagename>

它会很好地工作。

我假设您具有一个用户python python安装,并且不想打扰有关virtualenv(不是很用户友好)或pipenv的阅读

正如评论部分中的某些人指出的那样,除非您不知道该怎么办并陷入困境,否则下一个方法不是一个好主意:

针对全局包的另一种方法例如您要执行的操作:

chown -R $USER /Library/Python/2.7/site-packages/

或更一般地

chown -R $USER <path to your global pip packages>

Your original problem is that pip cannot write the logs to the folder.

IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

You need to cd into a folder in which the process invoked can write like /tmp so a cd /tmp and re invoking the command will probably work but is not what you want.

BUT actually for this particular case (you not wanting to use sudo for installing python packages) and no need for global package installs you can use the --user flag like this :

pip install --user <packagename>

and it will work just fine.

I assume you have a one user python python installation and do not want to bother with reading about virtualenv (which is not very userfriendly) or pipenv.

As some people in the comments section have pointed out the next approach is not a very good idea unless you do not know what to do and got stuck:

Another approach for global packages like in your case you want to do something like :

chown -R $USER /Library/Python/2.7/site-packages/

or more generally

chown -R $USER <path to your global pip packages>

回答 3

因为我遇到了同样的问题,所以我想强调一下,布莱恩·凯恩的第一条评论实际上是“ IOError:[Errno 13]”问题的解决方案:

如果在temp目录(cd /tmp)中执行,那么如果我运行IOError就不会再发生sudo pip install foo

Because I had the same problem, I want to stress that actually the first comment by Brian Cain is the solution to the “IOError: [Errno 13]”-problem:

If executed in the temp directory (cd /tmp), the IOError does not occur anymore if I run sudo pip install foo.


回答 4

virtualenvwrapper成功安装后,我在安装时遇到问题virtualenv

执行此操作后,我的终端抱怨:

pip install virtualenvwrapper

因此,我尝试失败(不推荐)

sudo pip install virtualenvwrapper

然后,我成功安装了它:

pip install --user virtualenvwrapper

I had a problem installing virtualenvwrapper after successfully installing virtualenv.

My terminal complained after I did this:

pip install virtualenvwrapper

So, I unsuccessfully tried this (NOT RECOMMENDED):

sudo pip install virtualenvwrapper

Then, I successfully installed it with this:

pip install --user virtualenvwrapper

回答 5

看来您的权限搞砸了。键入chown -R markwalker ~在终端和尝试pip一次?让我知道您是否已排序。

It looks like your permissions are messed up. Type chown -R markwalker ~ in the Terminal and try pip again? Let me know if you’re sorted.


在Mac OS X上的python位置

问题:在Mac OS X上的python位置

我对osx上的python感到有些困惑。我不知道笔记本电脑的先前所有者是否已使用macport安装了macpython。我记得osx具有内置版本的python。我尝试使用type -a python,结果返回

python is /usr/bin/python
python is /usr/local/bin/python

但是在这些位置运行两个python给了我[GCC 4.2.1 (Apple Inc. build 5646)] on darwin。它们是否都引用了提供的相同内置python mac?

我还读到安装macpython会

     A MacPython 2.5 folder in your Applications folder. In here you
 find IDLE, the development environment that is a standard part of
 official Python distributions...

我看了看Applications,里面有一个MacPort文件夹,上面python2.6有提到的东西。但是运行IDLE,我发现与上面相同的消息。

嗯,我有点困惑。哪一个

I’m a little confused with the python on osx. I do not know if the previous owner of the laptop has installed macpython using macport. And I remembered that osx has an builtin version of python. I tried using type -a python and the result returned

python is /usr/bin/python
python is /usr/local/bin/python

However running both python at these locations give me [GCC 4.2.1 (Apple Inc. build 5646)] on darwin. Do they both refer to the same builtin python mac provided?

I also read that installing macpython one would

     A MacPython 2.5 folder in your Applications folder. In here you
 find IDLE, the development environment that is a standard part of
 official Python distributions...

I looked at Applications, and theres a MacPort folder with python2.6 and the mentioned stuff in it. But running IDLE, i find the same message as above.

Hmm I’m a little confused. Which is which?


回答 0

[GCC 4.2.1 (Apple Inc. build 5646)]是构建Python的GCC版本,而不是Python本身的版本。该信息应在前一行。例如:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

/usr/bin除非某人不明智地更改了其中的内容,否则其中的项目应该始终是或链接到Apple在OS X中提供的文件。要确切了解/usr/local/bin/python链接到的位置:

$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jul  5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

在这种情况下,这对于python.org安装的Python实例来说是典型的,也可以是从源代码构建的。

[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:

# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:

$ ls -l /usr/local/bin/python
lrwxr-xr-x  1 root  wheel  68 Jul  5 10:05 /usr/local/bin/python@ -> ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/python

In this case, that is typical for a python.org installed Python instance or it could be one built from source.


回答 1

在Mac OS X上,它位于的Python框架中/System/Library/Frameworks/Python.framework/Resources

完整路径为:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

顺便说一句,很容易找到在哪里可以找到特定的二进制文件:which Python将向您显示Python二进制文件的路径(可能与我上面发布的相同)。

On Mac OS X, it’s in the Python framework in /System/Library/Frameworks/Python.framework/Resources.

Full path is:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Btw it’s easy to find out where you can find a specific binary: which Python will show you the path of your Python binary (which is probably the same as I posted above).


回答 2

我找到了最简单的定位方法,可以使用

which python

它会显示如下内容:

/usr/bin/python

I found the easiest way to locate it, you can use

which python

it will show something like this:

/usr/bin/python


回答 3

这个会 解决您所有的问题使用Python和Mac时:

如果您有Mac,并且像我们大多数人一样安装了python3:)(使用brew install-ofc)

您的文件位于:

/usr/local/Cellar/python/3.6.4_4/bin/python3

你怎么知道的?跑:

which python3

您应该得到:

/usr/local/bin/python3

现在这是一个符号链接,您怎么知道?跑:

ls -al /usr/local/bin/python3 

您会得到:

/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3

这意味着你

/usr/local/bin/python3 

实际上指向:

/usr/local/Cellar/python/3.6.4_4/bin/python3

如果由于某种原因

/usr/local/bin/python3 

没有指向您想要的地方,在我们的例子中:

/usr/local/Cellar/python/3.6.4_4/bin/python3

只需备份:

cp /usr/local/bin/python3{,.orig} 

并运行:

rm -rf /usr/local/bin/python3

现在创建一个新的符号链接:

ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3 

现在你的

/usr/local/bin/python3

指向

/usr/local/Cellar/python/3.6.4_4/bin/python3 

通过运行以下命令进行检查:

ls -al /usr/local/bin/python3

This one will solve all your problems dealing with Python on basic shell:

If you have a Mac and you’ve installed python3 like most of us do :) (with brew install – ofc)

your file is located in:

/usr/local/Cellar/python/3.6.4_4/bin/python3

How do you know? -> you can run this on every basic shell Run:

which python3

You should get:

/usr/local/bin/python3

Now this is a symbolic link, how do you know? Run:

ls -al /usr/local/bin/python3 

and you’ll get:

/usr/local/bin/python3 -> /usr/local/Cellar/python/3.6.4_4/bin/python3

which means that your

/usr/local/bin/python3 

is actually pointing to:

/usr/local/Cellar/python/3.6.4_4/bin/python3

If, for some reason, your

/usr/local/bin/python3 

is not pointing to the place you want, which in our case:

/usr/local/Cellar/python/3.6.4_4/bin/python3

just backup it:

cp /usr/local/bin/python3{,.orig} 

and run:

rm -rf /usr/local/bin/python3

now create a new symbolic link:

ln -s /usr/local/Cellar/python/3.6.4_4/bin/python3 /usr/local/bin/python3 

and now your

/usr/local/bin/python3

is pointing to

/usr/local/Cellar/python/3.6.4_4/bin/python3 

Check it out by running:

ls -al /usr/local/bin/python3

回答 4

在高山脉

which python

显示默认的python,但是如果您从python.org下载并安装了最新版本,则可以通过以下方式找到它:

which python3.6

我机器上显示的

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

On High Sierra

which python

shows the default python but if you downloaded and installed the latest version from python.org you can find it by:

which python3.6

which on my machine shows

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

回答 5

用“ brew install python3”安装,在这里找到

installed with ‘brew install python3’, found it here


回答 6

我检查了一些类似的讨论,发现找到所有python2 / python3构建的最佳方法是:

which -a python python3

I checked a few similar discussions and found out the best way to locate all python2/python3 builds is:

which -a python python3

回答 7

我在这里找到它:/Library/Frameworks/Python.framework/Versions/3.6/bin

i found it here: /Library/Frameworks/Python.framework/Versions/3.6/bin


回答 8

which python3 只会导致解释者在其中安定下来。

which python3 simply result in a path in which the interpreter settles down.


回答 9

在您的交互式终端中运行

import os
os.path

它将为您提供安装python的文件夹

Run this in your interactive terminal

import os
os.path

It will give you the folder where python is installed


回答 10

在.py文件中运行以下代码:

import sys

print(sys.version)
print(sys.executable)

run the following code in a .py file:

import sys

print(sys.version)
print(sys.executable)

回答 11

我有一个在Linux / macos中寻找东西的厨师食谱

首先更新定位数据库,然后执行

locate WHATiWANTtoSEARCH | less

/找到您想要的东西。

要在macOS中更新您的定位数据库,请执行以下操作:

sudo /usr/libexec/locate.updatedb

有时需要一段时间。希望这可以帮助 :)

I have a cook recipe for finding things in linux/macos

First update the locate db then do a

locate WHATiWANTtoSEARCH | less

do a /find to find what you are looking for.

to update your locate db in macos do this:

sudo /usr/libexec/locate.updatedb

it sometimes takes a while. Hope this helps :)


pip成功安装软件包,但在命令行中找不到可执行文件

问题:pip成功安装软件包,但在命令行中找不到可执行文件

我正在使用Mac OS X Yosemite版本10.10.3。

我在http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/中使用macport安装了python2.7和pip

我可以成功安装软件包并将其导入我的python环境和python脚本中。但是,找不到与可以从终端中的命令行调用的软件包相关联的可执行文件。

有谁知道可能出什么问题了?(下面有更多详细信息)

例如,按照http://wiki.ros.org/jade/Installation/Source中的指示安装名为“ rosdep”的软件包时

我可以运行:sudo pip install -U rosdep 安装没有错误,并且相应文件位于/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

但是,如果我尝试运行:sudo rosdep init,则会出现错误:"sudo: rosdep: command not found"

这不是程序包特定的错误。对于在计算机上使用pip安装的所有软件包,我都会得到它。我什至尝试添加

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

对我的$PATH。但是,即使这些程序包可以在python内正常运行,也无法在命令行上找到可执行文件。

I am working on mac OS X Yosemite, version 10.10.3.

I installed python2.7 and pip using macport as done in http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/

I can successfully install packages and import them inside my python environment and python scripts. However any executable associated with a package that can be called from the command line in the terminal are not found.

Does anyone know what might be wrong? (More details below)

For example while installing a package called “rosdep” as instructed in http://wiki.ros.org/jade/Installation/Source

I can run: sudo pip install -U rosdep which installs without errors and corresponding files are located in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

However if I try to run : sudo rosdep init, it gives an error : "sudo: rosdep: command not found"

This is not a package specific error. I get this for any package installed using pip on my computer. I even tried adding

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

to my $PATH. But the executables are not found on the command line, even though the packages work perfectly from within python.


回答 0

检查您的$ PATH

tox 具有命令行模式:

audrey:tests jluc$ pip list | grep tox
tox (2.3.1)

它在哪里?

(编辑:这里的2.7东西没什么大不了的,无论是sub 3.x还是pip的行为都差不多)

audrey:tests jluc$ which tox
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tox

我的$ PATH中有什么?

audrey:tests jluc$ echo $PATH
/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/local/bin:..../opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin...

注意/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin吗?这就是找到我点子安装的东西的原因

现在,看到那里的东西都是从Python中,尝试这样做(替代rosdeptox)。

$python
>>> import tox
>>> tox.__file__

打印出来:

'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tox/__init__.pyc'

现在,将cd转到上面上方的目录lib。您看到bin目录吗?你看到rosdep那个垃圾箱了吗?如果是这样,请尝试将添加bin到您的$ PATH中。

audrey:2.7 jluc$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7
audrey:2.7 jluc$ ls -1

输出:

Headers
Python
Resources
bin
include
lib
man
share

check your $PATH

tox has a command line mode:

audrey:tests jluc$ pip list | grep tox
tox (2.3.1)

where is it?

(edit: the 2.7 stuff doesn’t matter much here, sub in any 3.x and pip’s behaving pretty much the same way)

audrey:tests jluc$ which tox
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tox

and what’s in my $PATH?

audrey:tests jluc$ echo $PATH
/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/local/bin:..../opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin...

Notice the /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin? That’s what allows finding my pip-installed stuff

Now, to see where things are from Python, try doing this (substitute rosdep for tox).

$python
>>> import tox
>>> tox.__file__

that prints out:

'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tox/__init__.pyc'

Now, cd to the directory right above lib in the above. Do you see a bin directory? Do you see rosdep in that bin? If so try adding the bin to your $PATH.

audrey:2.7 jluc$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7
audrey:2.7 jluc$ ls -1

output:

Headers
Python
Resources
bin
include
lib
man
share

回答 1

在具有默认python安装的macOS上,您需要添加/Users/<you>/Library/Python/2.7/bin/到$ PATH中。

将此添加到您的.bash_profile:

export PATH="/Users/<you>/Library/Python/2.7/bin:$PATH"

这就是pip安装可执行文件的地方。

提示:对于非默认的python版本which python,请找到您的python安装位置,并在上面的路径中替换该部分。(感谢提示Sanket_Diwale)

On macOS with the default python installation you need to add /Users/<you>/Library/Python/2.7/bin/ to your $PATH.

Add this to your .bash_profile:

export PATH="/Users/<you>/Library/Python/2.7/bin:$PATH"

That’s where pip installs the executables.

Tip: For non-default python version which python to find the location of your python installation and replace that portion in the path above. (Thanks for the hint Sanket_Diwale)


回答 2

如果您使用--user(例如pip3.6 install --user tmuxp)进行安装,则可以使用sitemodule 从Python本身获取特定于平台的用户安装目录。例如,在macOS上:

$ python2.7 -m site --user-base
/Users/alexp/Library/Python/2.7

通过附加/bin此内容,我们现在有了安装包可执行文件的路径。我们可以根据输出动态在您的Shell的rc文件中填充PATH;我正在使用bash,但是幸运的是,它是可移植的:

# Add Python bin directories to path
python3.6 -m site &> /dev/null && PATH="$PATH:`python3.6 -m site --user-base`/bin"
python2.7 -m site &> /dev/null && PATH="$PATH:`python2.7 -m site --user-base`/bin"

我使用精确的Python版本来减少当Python将次版本(例如从3.5升级到3.6)升级时可执行文件“消失”的机会。它们将消失,因为如上所示,用户安装路径可能包括Python版本。因此,虽然python3可能指向3.5或3.6,但python3.6始终将指向3.6。在安装其他软件包时,例如pip3.6在over上使用时,请记住这一点pip3

如果您不介意软件包消失的想法,可以使用python2python3代替:

# Add Python bin directories to path
# Note: When Python is upgraded, packages may need to be re-installed
#       or Python versions managed.
python3 -m site &> /dev/null && PATH="$PATH:`python3 -m site --user-base`/bin"
python2 -m site &> /dev/null && PATH="$PATH:`python2 -m site --user-base`/bin"

If you’re installing using --user (e.g. pip3.6 install --user tmuxp), it is possible to get the platform-specific user install directory from Python itself using the site module. For example, on macOS:

$ python2.7 -m site --user-base
/Users/alexp/Library/Python/2.7

By appending /bin to this, we now have the path where package executables will be installed. We can dynamically populate the PATH in your shell’s rc file based on the output; I’m using bash, but with any luck this is portable:

# Add Python bin directories to path
python3.6 -m site &> /dev/null && PATH="$PATH:`python3.6 -m site --user-base`/bin"
python2.7 -m site &> /dev/null && PATH="$PATH:`python2.7 -m site --user-base`/bin"

I use the precise Python versions to reduce the chance of the executables just “disappearing” when Python upgrades a minor version, e.g. from 3.5 to 3.6. They’ll disappear because, as can be seen above, the user installation path may include the Python version. So while python3 could point to 3.5 or 3.6, python3.6 will always point to 3.6. This needs to be kept in mind when installing further packages, e.g. use pip3.6 over pip3.

If you don’t mind the idea of packages disappearing, you can use python2 and python3 instead:

# Add Python bin directories to path
# Note: When Python is upgraded, packages may need to be re-installed
#       or Python versions managed.
python3 -m site &> /dev/null && PATH="$PATH:`python3 -m site --user-base`/bin"
python2 -m site &> /dev/null && PATH="$PATH:`python2 -m site --user-base`/bin"

回答 3

我知道问题是关于macOS的,但是这是通过Google到达Linux的Linux用户的解决方案。

我已经通过pip 安装了pdfx软件包,因此遇到了这个问题中描述的问题。

但是,当我运行它时,什么都没有…

pip list | grep pdfx
pdfx (1.3.0)

然而:

which pdfx
pdfx not found

在Linux上的问题是 pip install ...将脚本放入其中~/.local/bin,而不是默认的Debian / Ubuntu上的脚本$PATH

以下是GitHub问题的详细信息: https //github.com/pypa/pip/issues/3813

要解决此问题,只需将添加~/.local/bin$PATH,例如,将以下行添加到.bashrc文件中:

export PATH="$HOME/.local/bin:$PATH"

在那之后,重新启动您的外壳,一切应该按预期进行。

I know the question asks about macOS, but here is a solution for Linux users who arrive here via Google.

I was having the issue described in this question, having installed the pdfx package via pip.

When I ran it however, nothing…

pip list | grep pdfx
pdfx (1.3.0)

Yet:

which pdfx
pdfx not found

The problem on Linux is that pip install ... drops scripts into ~/.local/bin and this is not on the default Debian/Ubuntu $PATH.

Here’s a GitHub issue going into more detail: https://github.com/pypa/pip/issues/3813

To fix, just add ~/.local/bin to your $PATH, for example by adding the following line to your .bashrc file:

export PATH="$HOME/.local/bin:$PATH"

After that, restart your shell and things should work as expected.


回答 4

基于其他的答案,对linuxmac您可以运行以下命令:

echo "export PATH=\"`python3 -m site --user-base`/bin:$PATH\"" >> ~/.bashrc
source ~/.bashrc

而不是python3你可以使用任何其他链接到Python版本:pythonpython2.7python3.6python3.9,等。

说明

为了知道用户软件包在当前操作系统(win,mac,linux)中的安装位置,我们运行:

python3 -m site --user-base

我们知道脚本会转到bin/安装软件包的文件夹。

因此,我们将路径连接起来:

echo `python3 -m site --user-base`/bin

然后我们将其导出到环境变量。

export PATH=\"`python3 -m site --user-base`/bin:$PATH\""

最后,为了避免重复执行export命令,我们将其添加到.bashrc文件中,然后运行source以运行新更改,从而为我们提供了开头提到的建议解决方案。

Solution

Based on other answers, for linux and mac you can run the following:

echo "export PATH=\"`python3 -m site --user-base`/bin:$PATH\"" >> ~/.bashrc
source ~/.bashrc

instead of python3 you can use any other link to python version: python, python2.7, python3.6, python3.9, etc.

Explanation

In order to know where the user packages are installed in the current OS (win, mac, linux), we run:

python3 -m site --user-base

We know that the scripts go to the bin/ folder where the packages are installed.

So we concatenate the paths:

echo `python3 -m site --user-base`/bin

Then we export that to an environment variable.

export PATH=\"`python3 -m site --user-base`/bin:$PATH\"

Finally, in order to avoid repeating the export command we add it to our .bashrc file and we run source to run the new changes, giving us the suggested solution mentioned at the beginning.


回答 5

在Windows上,您需要将路径添加%USERPROFILE%\AppData\Roaming\Python\Scripts到您的路径。

On Windows, you need to add the path %USERPROFILE%\AppData\Roaming\Python\Scripts to your path.


回答 6

我偶然发现了这个问题,因为我创建,成功构建并发布了一个PyPI软件包,但安装后无法执行。该$PATH变量被设置正确。

就我而言,问题是我没有entry_pointsetup.py文件中设置:

entry_points = {'console_scripts':

['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},

I stumbled upon this question because I created, successfully built and published a PyPI Package, but couldn’t execute it after installation. The $PATHvariable was correctly set.

In my case the problem was that I hadn’t set the entry_pointin the setup.py file:

entry_points = {'console_scripts':

['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},

回答 7

除了将python的bin目录添加到$PATH变量之外,我还必须更改该目录的所有者以使其正常运行。不知道为什么我还不是所有者。

chown -R ~/Library/Python/

In addition to adding python’s bin directory to $PATH variable, I also had to change the owner of that directory, to make it work. No idea why I wasn’t the owner already.

chown -R ~/Library/Python/

回答 8

当您使用MacOS安装程序(可从Python网站下载)安装Python或Python3时,它将export~/.profile脚本中添加er 。您只需要做source。重新启动所有终端也可以解决问题。

警告-我认为最好将pip3与Python3配合使用-以获取未来的收益。

如果您已经安装了Python3,则以下步骤可在macOS Mojave上为我工作:

  1. 首先使用sudo– 安装ansiblesudo -H pip3 install ansible

  2. 您创建到Python bin路径的符号链接

sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin

并装订到 .profile

export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin

  1. 运行source ~/.profile并重新启动所有终端外壳。

  2. 类型 ansible --version

When you install Python or Python3 using MacOS installer (downloaded from Python website) – it adds an exporter to your ~/.profile script. All you need to do is just source it. Restarting all the terminals should also do the trick.

WARNING – I believe it’s better to just use pip3 with Python3 – for future benefits.

If you already have Python3 installed, the following steps work for me on macOS Mojave:

  1. Install ansible first using sudosudo -H pip3 install ansible

  2. you create a symlink to the Python’s bin path

sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin

and staple it to .profile

export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin

  1. run source ~/.profile and restart all terminal shells.

  2. Type ansible --version


由于环境错误而无法安装软件包:[Errno 13]

问题:由于环境错误而无法安装软件包:[Errno 13]

在我的MacOS Mojave终端中,我想使用pip安装python软件包。最后说:

You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

所以我想用给定的命令更新点子,但出现错误:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 
'/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/EGG-INFO/PKG-INFO'
Consider using the `--user` option or check the permissions.

我真的不知道该怎么办。我也意识到它在错误消息中说Python 2.7,但是我已经并且只想使用python 3。

In my MacOS Mojave terminal I wanted to install a python package with pip. At the end it says:

You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

So I wanted to update pip with the given command but I got an error:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 
'/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/EGG-INFO/PKG-INFO'
Consider using the `--user` option or check the permissions.

I don’t really understand what to do now. Also I realized it says Python 2.7 in the error message but I have and want to use only python 3.


回答 0

如果要使用python3 +安装软件包,则需要使用pip3 install package_name

要解决errno 13,您必须--user在末尾添加

pip3 install package_name --user

编辑:

对于python中的任何项目,强烈建议Virtual enviroment上工作,该工具可通过为它们创建隔离的python虚拟环境来帮助将不同项目所需的依赖项分开。

为了使用python3 +创建一个,您必须使用以下命令:

virtualenv enviroment_name -p python3

然后只需激活它就可以对其进行处理:

source enviroment_name/bin/activate

激活虚拟环境后,虚拟环境的名称将显示在终端的左侧。这将使您知道虚拟环境当前处于活动状态。现在,您只需使用即可在该虚拟环境中安装与项目相关的依赖项pip

pip install package_name

If you want to use python3+ to install the packages you need to use pip3 install package_name

And to solve the errno 13 you have to add --user at the end

pip3 install package_name --user

EDIT:

For any project in python it’s highly recommended to work on a Virtual enviroment, is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.

In order to create one with python3+ you have to use the following command:

virtualenv enviroment_name -p python3

And then you work on it just by activating it:

source enviroment_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on left side of terminal. This will let you know that the virtual environment is currently active. Now you can install dependencies related to the project in this virtual environment by just using pip.

pip install package_name

回答 1

关于权限命令,请尝试在终端命令前使用sudo:

sudo pip install --upgrade pip

Sudo允许您使用超级用户的特权运行命令,并将为全局,系统范围的Python安装安装软件包。理想情况下,您应该为正在处理的项目创建一个虚拟环境。看看这个

关于python尝试将pip作为可执行文件运行,如下所示:

python3.6 -m pip install <package>

Regarding the permissions command, try using sudo in front of your terminal command:

sudo pip install --upgrade pip

Sudo is a program that allows you to run the command with the privileges of the superuser.

Regarding the python Try running pip as an executable like this:

python3.6 -m pip install <package>

回答 2

我犯了同样的错误,然后意识到我已经以root用户身份创建了虚拟环境。它已被写保护,因此请检查您的虚拟环境是否被写保护。制作新的venv,然后重试

I was making the same mistakes then I realized that I have created my virtual environment as root user. It was write protected, so please check whether your virtual environment is write protected. make a new venv and try again


回答 3

尝试安装软件包时(烧瓶类),我遇到了相同的错误。
我犯了以root身份安装anaconda的错误。我更改了已安装的anaconda文件夹的所有权,并且可以成功安装该软件包。

使用chown带选项的命令-R来递归地更改已安装的anaconda文件夹的所有权,如下所示:

chown -R owner:group /path/to/anaconda

在这里所有者是您的用户名,组是组名。

I got the same error when I was trying to install a package (flask-classful).
I made the mistake of installing anaconda as root. I changed the ownership of the installed anaconda folder and I could install the package successfully.

Use the command chown with option -R to recursively change ownership of the installed anaconda folder like so:

chown -R owner:group /path/to/anaconda

Here owner is your username and group is the group name.


回答 4

答案在错误消息中。过去,您或某个进程执行sudo pip,并且导致该目录下的某些目录/Library/Python/2.7/site-packages/...具有权限,导致当前用户无法访问该目录。

然后您做了一个pip install whatever依赖另一件事的。

因此,要对其进行修复,请访问/Library/Python/2.7/site-packages / …并找到具有root或not-your-user权限的目录,然后删除然后重新安装这些软件包,或者只是强制对用户拥有所有权应该接触的人。

The answer is in the error message. In the past you or a process did a sudo pip and that caused some of the directories under /Library/Python/2.7/site-packages/... to have permissions that make it unaccessable to your current user.

Then you did a pip install whatever which relies on the other thing.

So to fix it, visit the /Library/Python/2.7/site-packages/… and find the directory with the root or not-your-user permissions and either remove then reinstall those packages, or just force ownership to the user to whom ought to have access.


回答 5

使用进行安装时numpy,我遇到了同样的问题pip install numpy

然后我尝试

sudo -H pip3 install --upgrade pip

sudo -H pip3 install numpy

对我来说效果很好。

说明:-H带(HOME)选项sudo设置HOME环境变量设置为目标用户(root默认情况下)的主目录。默认情况下,sudo不会修改HOME。

I had the same problem while installing numpy with pip install numpy.

Then I tried

sudo -H pip3 install --upgrade pip

sudo -H pip3 install numpy

It worked well for me.

Explanation : The -H (HOME) option with sudo sets the HOME environment variable to the home directory of the target user (root by default). By default, sudo does not modify HOME.


回答 6

对于MacO和Unix

只需在命令中添加sudo即可,因为它将以超级用户身份运行。

sudo pip install --upgrade pip

建议您不要直接这样做-请参阅这篇文章

For MacOs & Unix

Just by adding sudo to command will work, as it would run it as a superuser.

sudo pip install --upgrade pip

It is advised that you should not directly do it though – please see this post


回答 7

这为我工作:

 python3 -m venv env
 source ./env/bin/activate
 python -m pip install package

(来自Github:https : //github.com/googlesamples/assistant-sdk-python/issues/236

This worked for me:

 python3 -m venv env
 source ./env/bin/activate
 python -m pip install package

(From Github: https://github.com/googlesamples/assistant-sdk-python/issues/236 )


回答 8

我已经尝试过此处发布的所有建议,但仍收到errno 13

我正在使用Windows,而我的python版本是3.7.3

经过5小时的尝试解决后,此步骤对我有用:

我尝试以管理员身份运行以打开命令提示符

I already tried all suggestion posted in here, yet I’m still getting the errno 13,

I’m using Windows and my python version is 3.7.3

After 5 hours of trying to solve it, this step worked for me:

I try to open the command prompt by run as administrator


回答 9

我也遇到了同样的问题,我尝试了许多不同的命令行,这对我有用:

尝试:

    conda install py-xgboost

那就是我得到的:

Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /home/simplonco/anaconda3

  added / updated specs:
    - py-xgboost


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _py-xgboost-mutex-2.0      |            cpu_0           9 KB
    ca-certificates-2019.1.23  |                0         126 KB
    certifi-2018.11.29         |           py37_0         146 KB
    conda-4.6.2                |           py37_0         1.7 MB
    libxgboost-0.80            |       he6710b0_0         3.7 MB
    mkl-2019.1                 |              144       204.6 MB
    mkl_fft-1.0.10             |   py37ha843d7b_0         169 KB
    mkl_random-1.0.2           |   py37hd81dba3_0         405 KB
    numpy-1.15.4               |   py37h7e9f1db_0          47 KB
    numpy-base-1.15.4          |   py37hde5b4d6_0         4.2 MB
    py-xgboost-0.80            |   py37he6710b0_0         1.7 MB
    scikit-learn-0.20.2        |   py37hd81dba3_0         5.7 MB
    scipy-1.2.0                |   py37h7c811a0_0        17.7 MB
    ------------------------------------------------------------
                                           Total:       240.0 MB

The following NEW packages will be INSTALLED:

  _py-xgboost-mutex  pkgs/main/linux-64::_py-xgboost-mutex-2.0-cpu_0
  libxgboost         pkgs/main/linux-64::libxgboost-0.80-he6710b0_0
  py-xgboost         pkgs/main/linux-64::py-xgboost-0.80-py37he6710b0_0

The following packages will be UPDATED:

  ca-certificates     anaconda::ca-certificates-2018.12.5-0 --> pkgs/main::ca-certificates-2019.1.23-0
  mkl                                            2019.0-118 --> 2019.1-144
  mkl_fft                              1.0.4-py37h4414c95_1 --> 1.0.10-py37ha843d7b_0
  mkl_random                           1.0.1-py37h4414c95_1 --> 1.0.2-py37hd81dba3_0
  numpy                               1.15.1-py37h1d66e8a_0 --> 1.15.4-py37h7e9f1db_0
  numpy-base                          1.15.1-py37h81de0dd_0 --> 1.15.4-py37hde5b4d6_0
  scikit-learn                        0.19.2-py37h4989274_0 --> 0.20.2-py37hd81dba3_0
  scipy                                1.1.0-py37hfa4b5c9_1 --> 1.2.0-py37h7c811a0_0

The following packages will be SUPERSEDED by a higher-priority channel:

  certifi                                          anaconda --> pkgs/main
  conda                                            anaconda --> pkgs/main
  openssl                anaconda::openssl-1.1.1-h7b6447c_0 --> pkgs/main::openssl-1.1.1a-h7b6447c_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
libxgboost-0.80      | 3.7 MB    | ##################################### | 100% 
mkl_random-1.0.2     | 405 KB    | ##################################### | 100% 
certifi-2018.11.29   | 146 KB    | ##################################### | 100% 
ca-certificates-2019 | 126 KB    | ##################################### | 100% 
conda-4.6.2          | 1.7 MB    | ##################################### | 100% 
mkl-2019.1           | 204.6 MB  | ##################################### | 100% 
mkl_fft-1.0.10       | 169 KB    | ##################################### | 100% 
numpy-1.15.4         | 47 KB     | ##################################### | 100% 
scipy-1.2.0          | 17.7 MB   | ##################################### | 100% 
scikit-learn-0.20.2  | 5.7 MB    | ##################################### | 100% 
py-xgboost-0.80      | 1.7 MB    | ##################################### | 100% 
_py-xgboost-mutex-2. | 9 KB      | ##################################### | 100% 
numpy-base-1.15.4    | 4.2 MB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

I also had the same problem, I tried many different command lines, this one worked for me:

Try:

    conda install py-xgboost

That’s what I got:

Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /home/simplonco/anaconda3

  added / updated specs:
    - py-xgboost


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _py-xgboost-mutex-2.0      |            cpu_0           9 KB
    ca-certificates-2019.1.23  |                0         126 KB
    certifi-2018.11.29         |           py37_0         146 KB
    conda-4.6.2                |           py37_0         1.7 MB
    libxgboost-0.80            |       he6710b0_0         3.7 MB
    mkl-2019.1                 |              144       204.6 MB
    mkl_fft-1.0.10             |   py37ha843d7b_0         169 KB
    mkl_random-1.0.2           |   py37hd81dba3_0         405 KB
    numpy-1.15.4               |   py37h7e9f1db_0          47 KB
    numpy-base-1.15.4          |   py37hde5b4d6_0         4.2 MB
    py-xgboost-0.80            |   py37he6710b0_0         1.7 MB
    scikit-learn-0.20.2        |   py37hd81dba3_0         5.7 MB
    scipy-1.2.0                |   py37h7c811a0_0        17.7 MB
    ------------------------------------------------------------
                                           Total:       240.0 MB

The following NEW packages will be INSTALLED:

  _py-xgboost-mutex  pkgs/main/linux-64::_py-xgboost-mutex-2.0-cpu_0
  libxgboost         pkgs/main/linux-64::libxgboost-0.80-he6710b0_0
  py-xgboost         pkgs/main/linux-64::py-xgboost-0.80-py37he6710b0_0

The following packages will be UPDATED:

  ca-certificates     anaconda::ca-certificates-2018.12.5-0 --> pkgs/main::ca-certificates-2019.1.23-0
  mkl                                            2019.0-118 --> 2019.1-144
  mkl_fft                              1.0.4-py37h4414c95_1 --> 1.0.10-py37ha843d7b_0
  mkl_random                           1.0.1-py37h4414c95_1 --> 1.0.2-py37hd81dba3_0
  numpy                               1.15.1-py37h1d66e8a_0 --> 1.15.4-py37h7e9f1db_0
  numpy-base                          1.15.1-py37h81de0dd_0 --> 1.15.4-py37hde5b4d6_0
  scikit-learn                        0.19.2-py37h4989274_0 --> 0.20.2-py37hd81dba3_0
  scipy                                1.1.0-py37hfa4b5c9_1 --> 1.2.0-py37h7c811a0_0

The following packages will be SUPERSEDED by a higher-priority channel:

  certifi                                          anaconda --> pkgs/main
  conda                                            anaconda --> pkgs/main
  openssl                anaconda::openssl-1.1.1-h7b6447c_0 --> pkgs/main::openssl-1.1.1a-h7b6447c_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
libxgboost-0.80      | 3.7 MB    | ##################################### | 100% 
mkl_random-1.0.2     | 405 KB    | ##################################### | 100% 
certifi-2018.11.29   | 146 KB    | ##################################### | 100% 
ca-certificates-2019 | 126 KB    | ##################################### | 100% 
conda-4.6.2          | 1.7 MB    | ##################################### | 100% 
mkl-2019.1           | 204.6 MB  | ##################################### | 100% 
mkl_fft-1.0.10       | 169 KB    | ##################################### | 100% 
numpy-1.15.4         | 47 KB     | ##################################### | 100% 
scipy-1.2.0          | 17.7 MB   | ##################################### | 100% 
scikit-learn-0.20.2  | 5.7 MB    | ##################################### | 100% 
py-xgboost-0.80      | 1.7 MB    | ##################################### | 100% 
_py-xgboost-mutex-2. | 9 KB      | ##################################### | 100% 
numpy-base-1.15.4    | 4.2 MB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

回答 10

MacOS上尝试以下命令行,以检查用户的权限。

$ sudo python -m pip install --user --upgrade pip

try this command line below for MacOS to check user’s permission.

$ sudo python -m pip install --user --upgrade pip

回答 11

我为Python 3安装了anaconda。我的mac也有Python2。

python --version

给我

的Python 3.7.3

python2.7 --version

给我

Python 2.7.10

我想在python2中安装pyspark软件包,因为它已经安装在python3中。

python2.7 -m pip install pyspark

给我一个错误

由于环境错误而无法安装软件包:[Errno 13]权限被拒绝:’/Library/Python/2.7/site-packages/pyspark’考虑使用该--user选项或检查权限。

下面的命令解决了它。谢谢上帝,我不必做任何配置更改。

python2.7 -m pip install pyspark --user

收集pyspark的要求已经满足:/Library/Python/2.7/site-packages中的py4j == 0.10.7(来自pyspark)(0.10.7)安装收集的软件包:pyspark成功安装pyspark-2.4.4您正在使用pip版本18.1。 ,但是版本19.3.1可用。您应该考虑通过“ pip install –upgrade pip”命令进行升级。

I have anaconda installed for Python 3. I also have Python2 in my mac.

python --version

gives me

Python 3.7.3

python2.7 --version

gives me

Python 2.7.10

I wanted to install pyspark package in python2, given that it was already installed in python3.

python2.7 -m pip install pyspark

gives me an error

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/Library/Python/2.7/site-packages/pyspark’ Consider using the --user option or check the permissions.

The below command solved it. Thank god I didn’t have to do any config changes.

python2.7 -m pip install pyspark --user

Collecting pyspark Requirement already satisfied: py4j==0.10.7 in /Library/Python/2.7/site-packages (from pyspark) (0.10.7) Installing collected packages: pyspark Successfully installed pyspark-2.4.4 You are using pip version 18.1, however version 19.3.1 is available. You should consider upgrading via the ‘pip install –upgrade pip’ command.


回答 12

我在具有所有正确权限的linux上已安装NTFS分区上的venv中遇到了类似的麻烦。确保使用–ignore-installed运行了pip可以解决该问题,即:

python -m pip install --upgrade --ignore-installed

I had similar trouble in a venv on a mounted NTFS partition on linux with all the right permissions. Making sure pip ran with –ignore-installed solved it, i.e.:

python -m pip install --upgrade --ignore-installed


回答 13

在Mac上,没有3.7目录,或者目录3.7归拥有root。因此,我删除了该目录,由当前用户创建了一个新目录,并将其移至该目录。然后安装完成,没有错误。

sudo rm -rf /Library/Python/3.7
mkdir 3.7
sudo mv 3.7 /Library/Python
ll /Library/Python/
pip3 install numpy

On Mac, there is no 3.7 directory or the directory 3.7 is owned by root. So, I removed that directory, create a new directory by current user, and move it there. Then installation finishes without error.

sudo rm -rf /Library/Python/3.7
mkdir 3.7
sudo mv 3.7 /Library/Python
ll /Library/Python/
pip3 install numpy

回答 14

当我尝试安装opencv-python软件包时,也会发生这种情况:

我可以用命令行修复它

python3 -m pip install {name of package} --user

当我尝试安装上述软件包时,命令变为:

python3 -m pip install opencv-python --user

结果是:

This also happens to me when I try to install the opencv-python package:

I can fix it with command line

python3 -m pip install {name of package} --user

When I try to install the said package, the command becomes:

python3 -m pip install opencv-python --user

Resulting in this:


回答 15

只是 sudo pip install packagename

just sudo pip install packagename


如何更改默认的python版本?

问题:如何更改默认的python版本?

我已经在Mac中安装了python 3.2。运行/ Applications / Python 3.2 / Update Shell Profile.command之后,令人困惑的是,当我在Terminal中键入python -V时,它说Python 2.6.1,如何更改默认的python版本?

I have installed python 3.2 in my mac. After I run /Applications/Python 3.2/Update Shell Profile.command, it’s confusing that when I type python -V in Terminal it says that Python 2.6.1, how can I change the default python version?


回答 0

这可能是向后兼容所希望的。

Python3破坏了向后兼容性,并且调用“ python”的程序可能希望使用python2。您可能有很多程序和脚本,甚至都不知道哪个期望使用python = python2,更改此设置将破坏这些程序和脚本。

您可能正在寻找的答案是:您不应该更改此设置

但是,您可以在shell中创建自定义别名。您这样做的方式取决于外壳,但也许您可以这样做alias py=python3

如果您对如何启动最新版本的python感到困惑,则至少在Linux上可以python3使python2安装保持完整(由于上述兼容性原因);因此,您可以使用以下python3命令启动python3 。

This is probably desirable for backwards compatibility.

Python3 breaks backwards compatibility, and programs invoking ‘python’ probably expect python2. You probably have many programs and scripts which you are not even aware of which expect python=python2, and changing this would break those programs and scripts.

The answer you are probably looking for is You should not change this.

You could, however, make a custom alias in your shell. The way you do so depends on the shell, but perhaps you could do alias py=python3

If you are confused about how to start the latest version of python, it is at least the case on Linux that python3 leaves your python2 installation intact (due to the above compatibility reasons); thus you can start python3 with the python3 command.


回答 1

检查python 3的位置

$ which python3
/usr/local/bin/python3

在bash_profile中写别名

vi ~/.bash_profile  
alias python='/usr/local/bin/python3'

重新加载bash_profile

source ~/.bash_profile

确认python命令

$ python --version
Python 3.6.5

Check the location of python 3

$ which python3
/usr/local/bin/python3

Write alias in bash_profile

vi ~/.bash_profile  
alias python='/usr/local/bin/python3'

Reload bash_profile

source ~/.bash_profile

Confirm python command

$ python --version
Python 3.6.5

回答 2

在Mac OS X上,如您所见,使用python.org安装程序,您需要使用python3,而不是调用Python 3 python。目前仅保留用于Python 2版本。您也可以python3.2用来专门调用该版本。

$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 2to3@ -> 2to3-3.2
-rwxrwxr-x  1 root  admin    140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x  1 root  admin      7 Apr 28 15:51 idle3@ -> idle3.2
-rwxrwxr-x  1 root  admin    138 Feb 20 11:14 idle3.2*
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 pydoc3@ -> pydoc3.2
-rwxrwxr-x  1 root  admin    123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3*
lrwxr-xr-x  1 root  admin     12 Apr 28 15:51 python3-32@ -> python3.2-32
lrwxr-xr-x  1 root  admin     16 Apr 28 15:51 python3-config@ -> python3.2-config
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x  1 root  admin     17 Apr 28 15:51 python3.2-config@ -> python3.2m-config
-rwxrwxr-x  1 root  admin  25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x  1 root  admin   1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x  1 root  admin     10 Apr 28 15:51 pythonw3@ -> pythonw3.2
lrwxr-xr-x  1 root  admin     13 Apr 28 15:51 pythonw3-32@ -> pythonw3.2-32
-rwxrwxr-x  1 root  admin  25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 pythonw3.2-32*

如果您还从python.org安装了Python 2,它将有一个类似的框架bin目录,没有重叠的文件名(2to3除外)。

$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x  1 root  admin    150 Jul  3  2010 2to3*
lrwxr-x---  1 root  admin      7 Nov  8 23:14 idle@ -> idle2.7
-rwxrwxr-x  1 root  admin    138 Jul  3  2010 idle2.7*
lrwxr-x---  1 root  admin      8 Nov  8 23:14 pydoc@ -> pydoc2.7
-rwxrwxr-x  1 root  admin    123 Jul  3  2010 pydoc2.7*
lrwxr-x---  1 root  admin      9 Nov  8 23:14 python@ -> python2.7
lrwxr-x---  1 root  admin     16 Nov  8 23:14 python-config@ -> python2.7-config
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 python2.7*
-rwxrwxr-x  1 root  admin   1663 Jul  3  2010 python2.7-config*
lrwxr-x---  1 root  admin     10 Nov  8 23:14 pythonw@ -> pythonw2.7
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 pythonw2.7*
lrwxr-x---  1 root  admin     11 Nov  8 23:14 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x  1 root  admin  18272 Jul  3  2010 smtpd2.7.py*

On Mac OS X using the python.org installer as you apparently have, you need to invoke Python 3 with python3, not python. That is currently reserved for Python 2 versions. You could also use python3.2 to specifically invoke that version.

$ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ cd /Library/Frameworks/Python.framework/Versions/3.2/bin/
$ ls -l
total 384
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 2to3@ -> 2to3-3.2
-rwxrwxr-x  1 root  admin    140 Feb 20 11:14 2to3-3.2*
lrwxr-xr-x  1 root  admin      7 Apr 28 15:51 idle3@ -> idle3.2
-rwxrwxr-x  1 root  admin    138 Feb 20 11:14 idle3.2*
lrwxr-xr-x  1 root  admin      8 Apr 28 15:51 pydoc3@ -> pydoc3.2
-rwxrwxr-x  1 root  admin    123 Feb 20 11:14 pydoc3.2*
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3*
lrwxr-xr-x  1 root  admin     12 Apr 28 15:51 python3-32@ -> python3.2-32
lrwxr-xr-x  1 root  admin     16 Apr 28 15:51 python3-config@ -> python3.2-config
-rwxrwxr-x  2 root  admin  25624 Feb 20 11:14 python3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 python3.2-32*
lrwxr-xr-x  1 root  admin     17 Apr 28 15:51 python3.2-config@ -> python3.2m-config
-rwxrwxr-x  1 root  admin  25784 Feb 20 11:14 python3.2m*
-rwxrwxr-x  1 root  admin   1865 Feb 20 11:14 python3.2m-config*
lrwxr-xr-x  1 root  admin     10 Apr 28 15:51 pythonw3@ -> pythonw3.2
lrwxr-xr-x  1 root  admin     13 Apr 28 15:51 pythonw3-32@ -> pythonw3.2-32
-rwxrwxr-x  1 root  admin  25624 Feb 20 11:14 pythonw3.2*
-rwxrwxr-x  1 root  admin  13964 Feb 20 11:14 pythonw3.2-32*

If you also installed a Python 2 from python.org, it would have a similar framework bin directory with no overlapping file names (except for 2to3).

$ open /Applications/Python\ 2.7/Update\ Shell\ Profile.command
$ sh -l
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ which python3
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ cd /Library/Frameworks/Python.framework/Versions/2.7/bin
$ ls -l
total 288
-rwxrwxr-x  1 root  admin    150 Jul  3  2010 2to3*
lrwxr-x---  1 root  admin      7 Nov  8 23:14 idle@ -> idle2.7
-rwxrwxr-x  1 root  admin    138 Jul  3  2010 idle2.7*
lrwxr-x---  1 root  admin      8 Nov  8 23:14 pydoc@ -> pydoc2.7
-rwxrwxr-x  1 root  admin    123 Jul  3  2010 pydoc2.7*
lrwxr-x---  1 root  admin      9 Nov  8 23:14 python@ -> python2.7
lrwxr-x---  1 root  admin     16 Nov  8 23:14 python-config@ -> python2.7-config
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 python2.7*
-rwxrwxr-x  1 root  admin   1663 Jul  3  2010 python2.7-config*
lrwxr-x---  1 root  admin     10 Nov  8 23:14 pythonw@ -> pythonw2.7
-rwxrwxr-x  1 root  admin  33764 Jul  3  2010 pythonw2.7*
lrwxr-x---  1 root  admin     11 Nov  8 23:14 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x  1 root  admin  18272 Jul  3  2010 smtpd2.7.py*

回答 3

老问题了,但是或者:

virtualenv --python=python3.5 .venv
source .venv/bin/activate

Old question, but alternatively:

virtualenv --python=python3.5 .venv
source .venv/bin/activate

回答 4

做正确的事,做正确的事!

—>零打开您的终端,

-首先输入python -V,它可能显示:

Python 2.7.10

-其次输入python3 -V,它可能显示:

Python 3.7.2

-三次输入where pythonwhich python,它可能显示:

/usr/bin/python

—第四个输入where python3which python3,它可能显示:

/usr/local/bin/python3

-第五,~/.profile file or ~/.bash_profile在Bash或~/.zshrczsh 下的PATH环境变量文件的底部添加以下行。

alias python='/usr/local/bin/python3'

要么

alias python=python3

source ~/.bash_profile在Bash或source ~/.zshrczsh 下第六次输入。

-第七步退出终端。

—第八次打开终端,输入python -V,可能显示:

Python 3.7.2

我已经成功地尝试过了。

其他的,~/.bash_profile不是zsh下的那个~/.bash_profile

zsh下的PATH环境变量~/.profile(或~/.bash_file通过)~/.zshrc

帮助你们!

Do right thing, do thing right!

—>Zero Open your terminal,

–Firstly input python -V, It likely shows:

Python 2.7.10

-Secondly input python3 -V, It likely shows:

Python 3.7.2

–Thirdly input where python or which python, It likely shows:

/usr/bin/python

—Fourthly input where python3 or which python3, It likely shows:

/usr/local/bin/python3

–Fifthly add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.

alias python='/usr/local/bin/python3'

OR

alias python=python3

-Sixthly input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.

–Seventhly Quit the terminal.

—Eighthly Open your terminal, and input python -V, It likely shows:

Python 3.7.2

I had done successfully try it.

Others, the ~/.bash_profile under zsh is not that ~/.bash_profile.

The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.

Help you guys!


回答 5

通过将“默认” Python放在您路径上的系统Python之前来进行更改,例如:

export PATH=/usr/local/bin:$PATH

Change the “default” Python by putting it ahead of the system Python on your path, for instance:

export PATH=/usr/local/bin:$PATH

回答 6

根据Google的快速搜索,此更新仅适用于您打开的当前Shell。可以通过键入python3来修复它,因为mac和linux足够相似,可以使这种情况重合。链接到Google搜索的结果。

而且,正如ninjagecko所说,大多数程序尚未更新到3.x,因此将默认python设置为3.x会破坏应用程序中使用的许多python脚本。

According to a quick google search, this update only applies to the current shell you have open. It can probably be fixed by typing python3, as mac and linux are similar enough for things like this to coincide. Link to the result of google search.

Also, as ninjagecko stated, most programs have not been updated to 3.x yet, so having the default python as 3.x would break many python scripts used in applications.


回答 7

我正在使用OS X 10.7.5和Python 3.4.2。如果键入python3并要运行什么,它将使用python 3运行它。例如 pyhton3 test001.py。那运行了我制作的名为test001的测试程序。我希望这有帮助。

I am using OS X 10.7.5 and Python 3.4.2. If you type python3 and what you want to run it will run it using python 3. For example pyhton3 test001.py. That ran a test program I made called test001. I hope this helps.


回答 8

导航:

我的电脑->属性->高级->环境变量->系统变量

假设您已经在路径变量中添加了python 2.7,并且您想将默认路径更改为python 3.x

然后在python2.7路径之前添加python3.5.x文件夹的路径。

打开cmd:键入“ python –version”

python版本将更改为python 3.5.x

Navigate to:

My Computer -> Properties -> Advanced -> Environment Variables -> System Variables

Suppose you had already having python 2.7 added in path variable and you want to change default path to python 3.x

then add path of python3.5.x folder before python2.7 path.

open cmd: type “python –version”

python version will be changed to python 3.5.x


回答 9

sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python

这将破坏脚本,但这正是更改python的方法。您还应该重写脚本以不假定python为2.x。无论您打电话system到哪个地方,它都将起作用exec

sudo mv /usr/bin/python /usr/bin/python2
sudo ln -s $(which python3) /usr/bin/python

This will break scripts, but is exactly the way to change python. You should also rewrite the scripts to not assume python is 2.x. This will work regardless of the place where you call system or exec.


回答 10

简而言之:更改环境变量中的路径!

对于Windows:

  • 进阶System Settings > Advance (tab)。在底部,您会发现'Environment Variables'

  • 双击Path。您将看到python安装之一的路径,将其更改为所需版本的路径。

In short: change the path in Environment Variables!

For Windows:

  • Advanced System Settings > Advance (tab). On bottom you’ll find 'Environment Variables'

  • Double-click on the Path. You’ll see path to one of the python installations, change that to path of your desired version.


回答 11

检查python3具有库的执行路径

$ which python3
/usr/local/bin/python3  some OS might have /usr/bin/python3

打开bash_profile文件并添加别名

vi ~/.bash_profile  
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'

重新加载bash_profile以使修改生效

source ~/.bash_profile

运行python命令并检查是否正在使用python3加载

$ python --version
Python 3.6.5

Check the execution path of python3 where it has libraries

$ which python3
/usr/local/bin/python3  some OS might have /usr/bin/python3

open bash_profile file and add an alias

vi ~/.bash_profile  
alias python='/usr/local/bin/python3' or alias python='/usr/bin/python3'

Reload bash_profile to take effect of modifications

source ~/.bash_profile

Run python command and check whether it’s getting loading with python3

$ python --version
Python 3.6.5

使用任何hg mercurial命令时,“找不到哈希md5的ERROR:root:code”

问题:使用任何hg mercurial命令时,“找不到哈希md5的ERROR:root:code”

尝试hg在控制台上使用任何Mercurial命令时,我一直收到此错误。我使用Homebrew安装了Python,并且正在运行Mac OS Catalina v.1.5.1。

任何参考将不胜感激。这是我得到的错误:

hg commit --amend
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/usr/local/bin/hg", line 43, in <module>
    dispatch.run()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 625, in <module>
    class lazyaliasentry(object):
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 636, in lazyaliasentry
    @util.propertycache
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/util.py", line 180, in <module>
    'md5': hashlib.md5,
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 151, in __getattr__
    return getattr(self._module, attr)
AttributeError: 'module' object has no attribute 'md5'

我也尝试按照此问题上的说明进行操作,但所有解决方案似乎均不起作用

brew link openssl --force
Warning: Refusing to link macOS-provided software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

When trying to use any hg Mercurial commands on the console, I keep getting this error. I installed Python using Homebrew and I am running Mac OS Catalina v. 10.15.1.

Any reference would be appreciated. Here is the error I’m getting:

hg commit --amend
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/usr/local/bin/hg", line 43, in <module>
    dispatch.run()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 625, in <module>
    class lazyaliasentry(object):
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/dispatch.py", line 636, in lazyaliasentry
    @util.propertycache
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 150, in __getattr__
    self._load()
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 94, in _load
    _origimport, head, globals, locals, None, level)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 43, in _hgextimport
    return importfunc(name, globals, *args, **kwargs)
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/mercurial/util.py", line 180, in <module>
    'md5': hashlib.md5,
  File "/usr/local/Cellar/mercurial/4.9/lib/python2.7/site-packages/hgdemandimport/demandimportpy2.py", line 151, in __getattr__
    return getattr(self._module, attr)
AttributeError: 'module' object has no attribute 'md5'

I also tried following the instruction on this issue but none of the solutions seem to work

brew link openssl --force
Warning: Refusing to link macOS-provided software: openssl@1.1
If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

回答 0

brew reinstall python@2对于我现有的Python 2.7虚拟环境,运行不起作用。在他们里面仍然有ERROR:root:code for hash sha1 was not found错误。

我跑步后遇到了这个问题brew upgrade openssl。解决方法是:

$ ls /usr/local/Cellar/openssl

…这表明

1.0.2t

根据现有版本,运行:

$ brew switch openssl 1.0.2t

…这表明

Cleaning /usr/local/Cellar/openssl/1.0.2t
Opt link created for /usr/local/Cellar/openssl/1.0.2t

之后,在Python 2.7 virtualenv中运行以下命令:

(my-venv) $ python -c "import hashlib;m=hashlib.md5();print(m.hexdigest())"

…这表明

d41d8cd98f00b204e9800998ecf8427e

没有更多的错误。

Running brew reinstall python@2 didn’t work for my existing Python 2.7 virtual environments. Inside them there were still ERROR:root:code for hash sha1 was not found errors.

I encountered this problem after I ran brew upgrade openssl. And here’s the fix:

$ ls /usr/local/Cellar/openssl

…which shows

1.0.2t

According to the existing version, run:

$ brew switch openssl 1.0.2t

…which shows

Cleaning /usr/local/Cellar/openssl/1.0.2t
Opt link created for /usr/local/Cellar/openssl/1.0.2t

After that, run the following command in a Python 2.7 virtualenv:

(my-venv) $ python -c "import hashlib;m=hashlib.md5();print(m.hexdigest())"

…which shows

d41d8cd98f00b204e9800998ecf8427e

No more errors.


回答 1

通过首先取消链接openssl来解决此问题

brew unlink openssl

然后重新安装python

brew reinstall python@2

我还注意到,在运行“ brew doctor”时,在/ usr / local / include / node /中有一个与openssl文件夹有关的警告。我在运行上述命令之前删除了此文件夹(不确定是否相关)

Managed to fix this by first unlinking openssl

brew unlink openssl

And then reinstalling python

brew reinstall python@2

I also noticed that when running ‘brew doctor’ there was a warning related to an openssl folder found in /usr/local/include/node/. I deleted this folder before running the above commands (not sure if related)


回答 2

对我来说,当我安装django网络应用程序的依赖项时,会弄乱环境。当我打字cd,它显示相同的错误。

问题是openssl图书馆,找不到正确的图书馆。

如果您使用的是Macintosh,则可以输入

ls /usr/local/Cellar/openssl

查看所有版本,

brew switch openssl 1.0.XXXX

选择可用的openssl版本。

然后错误消失了:)

The case for me is that when I install dependencies of a django web app, it messes up the environment. When I type cd, it shows the same error.

The problem was the openssl library, it can not find the correct ones.

If you are on Macintosh, you can type

ls /usr/local/Cellar/openssl

to see all the versions,

brew switch openssl 1.0.XXXX

to choose the available openssl version.

Then the error is gone :)


回答 3

当我导入hashlib时,会看到一条错误消息,指出未找到哈希md5。

我能够通过首先取消链接openssl来解决此问题:brew unlink openssl

然后,我使用MacPorts卸载了python 2.7:sudo port卸载python27

然后我使用MacPorts安装了python 2.7:sudo port install python27

现在导入hashlib可以工作了:)

When I would import hashlib I would see an error message stating that hash md5 was not found.

I was able to fix this problem by first unlinking openssl: brew unlink openssl

Then I uninstalled python 2.7 using MacPorts: sudo port uninstall python27

Then I installed python 2.7 using MacPorts: sudo port install python27

Now importing hashlib works :)


回答 4

只需卸载python2

$ brew uninstall python@2

如果有任何错误:

$ brew uninstall --ignore-dependencies python@2

Just uninstall python2

$ brew uninstall python@2

If there is any error :

$ brew uninstall --ignore-dependencies python@2


如何在Mac OS X上安装MySQLdb(MySQL的Python数据访问库)?

问题:如何在Mac OS X上安装MySQLdb(MySQL的Python数据访问库)?

我是Python的新手,但是我只花了一天的时间来研究如何使MySQLdb正常工作,而根据Google的说法,Universe包含了许多有关PITA是什么的参考,并且似乎有大量的指南。过时的。鉴于此站点旨在解决此类问题,并且我知道将来需要该解决方案的参考,因此我将提出问题,提供答案并查看还有哪些问题表面。

所以问题是如何使MySQLdb在Mac OS X上运行?

I’m a Python newbie, but I’ve just spent a day working out how to get MySQLdb working properly, and the universe according to google includes numerous references to what a PITA it is, and an inordinate number of guides that seem to be outdated. Given that this site is intended to address these sorts of problems, and I know that I’m going to need a reference to the solution in future, I’m going to ask the question, provide my answer and see what else floats to the surface.

So the question is how to get MySQLdb working on Mac OS X?


回答 0

使用Python3的用户的更新: 您可以简单地conda install mysqlclient用来安装使用MySQLdb所需的库,因为它目前存在。以下SO问题是一个有用的线索:Python 3 ImportError:没有名为’ConfigParser’的模块。安装mysqlclient将安装mysqlclient,mysql-connector和llvmdev(至少在我的机器上安装了这3个库)。

这是我关于这个问题的漫漫经历的故事。如果您对问题有更好的经验,不妨看一下它的编辑或概括…应用一下这种SO魔术。

注意:下一段中的注释适用于Snow Leopard,但不适用于Lion,后者似乎需要64位MySQL

首先,MySQLdb的作者(还是?)在这里说,最有害的问题之一是OS X附带了32位版本的Python,但是大多数普通人(包括我自己)都可能安装了64位版本。 MySQL。不好动…删除64位版本,如果你已经安装了它(这个繁琐的任务指令适用于SO 这里),然后下载并(包安装32位版本在这里

关于如何构建和安装MySQLdb库,有许多分步说明。它们通常有细微的差异。 对我来说似乎最受欢迎,并提供了可行的解决方案。我在下面进行了一些修改,复制了它

步骤0: 开始之前,我假设您在Mac上安装了MySQL,Python和GCC

步骤1:从SourceForge 下载最新的MySQL for Python适配器

步骤2:解 压缩下载的软件包:

tar xzvf MySQL-python-1.2.2.tar.gz

步骤3: 在文件夹内,清洁包装:

sudo python setup.py clean

额外步骤,(来自此评论

步骤3b: 删除MySQL-python-1.2.2 / build / *目录下的所有内容-不要相信“ python setup.py clean”为您完成此操作

步骤3c: 删除Users / $ USER / .python-eggs下的鸡蛋

步骤4: 最初需要编辑_mysql.c,但现在不再需要。MySQLdb社区似乎已经修复了此错误。

步骤5: 在lib下创建一个符号链接,以指向一个名为mysql的子目录。这是编译期间要查找的位置。

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

步骤6: 编辑setup_posix.py并更改以下内容

mysql_config.path =“ mysql_config”

mysql_config.path =“ / usr / local / mysql / bin / mysql_config”

步骤7: 在同一个目录中,重建您的软件包(忽略软件包附带的警告)

sudo python setup.py build

步骤8: 安装软件包,您已完成。

sudo python setup.py install

第9步: 测试它是否正常工作。如果可以导入MySQLdb,则可以使用。

python

>>> import MySQLdb

步骤10: 如果在尝试导入时收到错误消息,提示Library not loaded: libmysqlclient.18.dylib以以下结尾:Reason: image not found您需要创建一个附加的符号链接,即:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

然后,您应该能够import MySQLdb没有任何错误。

最后一个麻烦是,如果您从构建目录启动Python,则会出现此错误:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3:用户警告:模块_mysql已从/Library/Python/2.5/中导入site-packages / MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg / _mysql.pyc,但XXXX / MySQL-python-1.2.3c1已添加到sys.path

这对Google来说很容易,但是要为您省去麻烦,您将最终在这里(或者可能不是……不是特别适合未来的URL),并发现您需要cd ..脱离构建目录,并且错误应该消失。

正如我在顶部写的那样,我很乐意看到这个答案具有普遍性,因为对于这个可怕的问题还有很多其他的具体经验。编辑掉,或者提供自己的更好的答案。

Update for those using Python3: You can simply use conda install mysqlclient to install the libraries required to use MySQLdb as it currently exists. The following SO question was a helpful clue: Python 3 ImportError: No module named ‘ConfigParser’ . Installing mysqlclient will install mysqlclient, mysql-connector, and llvmdev (at least, it installed these 3 libraries on my machine).

Here is the tale of my rambling experience with this problem. Would love to see it edited or generalised if you have better experience of the issue… apply a bit of that SO magic.

Note: Comments in next paragraph applied to Snow Leopard, but not to Lion, which appears to require 64-bit MySQL

First off, the author (still?) of MySQLdb says here that one of the most pernicious problems is that OS X comes installed with a 32 bit version of Python, but most average joes (myself included) probably jump to install the 64 bit version of MySQL. Bad move… remove the 64 bit version if you have installed it (instructions on this fiddly task are available on SO here), then download and install the 32 bit version (package here)

There are numerous step-by-steps on how to build and install the MySQLdb libraries. They often have subtle differences. This seemed the most popular to me, and provided the working solution. I’ve reproduced it with a couple of edits below

Step 0: Before I start, I assume that you have MySQL, Python, and GCC installed on the mac.

Step 1: Download the latest MySQL for Python adapter from SourceForge.

Step 2: Extract your downloaded package:

tar xzvf MySQL-python-1.2.2.tar.gz

Step 3: Inside the folder, clean the package:

sudo python setup.py clean

COUPLE OF EXTRA STEPS, (from this comment)

Step 3b: Remove everything under your MySQL-python-1.2.2/build/* directory — don’t trust the “python setup.py clean” to do it for you

Step 3c: Remove the egg under Users/$USER/.python-eggs

Step 4: Originally required editing _mysql.c, but is now NO LONGER NECESSARY. MySQLdb community seem to have fixed this bug now.

Step 5: Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Step 6: Edit the setup_posix.py and change the following

mysql_config.path = “mysql_config”

to

mysql_config.path = “/usr/local/mysql/bin/mysql_config”

Step 7: In the same directory, rebuild your package (ignore the warnings that comes with it)

sudo python setup.py build

Step 8: Install the package and you are done.

sudo python setup.py install

Step 9: Test if it’s working. It works if you can import MySQLdb.

python

>>> import MySQLdb

Step 10: If upon trying to import you receive an error complaining that Library not loaded: libmysqlclient.18.dylib ending with: Reason: image not found you need to create one additional symlink which is:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

You should then be able to import MySQLdb without any errors.

One final hiccup though is that if you start Python from the build directory you will get this error:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but XXXX/MySQL-python-1.2.3c1 is being added to sys.path

This is pretty easy to Google, but to save you the trouble you will end up here (or maybe not… not a particularly future-proof URL) and figure out that you need to cd .. out of build directory and the error should disappear.

As I wrote at the top, I’d love to see this answer generalised, as there are numerous other specific experiences of this horrible problem out there. Edit away, or provide your own, better answer.


回答 1

适用于Mac OS X 10.8(Mountain Lion),10.9(Mavericks),10.10(Yosemite)和10.11(El Capitan)的快速简便的方法:

我假设您已经安装了XCode,其命令行工具,Python和MySQL。

  1. 安装PIP:

    sudo easy_install pip
  2. 编辑〜/ .profile :(在Mac OS X 10.10中可能不需要)

    nano ~/.profile

    复制并粘贴以下两行

    export PATH=/usr/local/mysql/bin:$PATH
    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

    保存并退出。后记执行以下命令:

    source  ~/.profile
  3. 安装MySQLdb

    sudo pip install MySQL-python

    要测试一切是否正常,请尝试

    python -c "import MySQLdb"

对我来说,它就像是一种魅力。希望对您有所帮助。

如果遇到有关缺少库的错误:库未加载:libmysqlclient.18.dylib,则必须将其符号链接为/usr/lib

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 

A quick and easy way for Mac OS X 10.8 (Mountain Lion), 10.9 (Mavericks), 10.10 (Yosemite), and 10.11 (El Capitan):

I assume you have XCode, its command line tools, Python, and MySQL installed.

  1. Install PIP:

    sudo easy_install pip
    
  2. Edit ~/.profile: (Might not be necessary in Mac OS X 10.10)

    nano ~/.profile
    

    Copy and paste the following two line

    export PATH=/usr/local/mysql/bin:$PATH
    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
    

    Save and exit. Afterwords execute the following command:

    source  ~/.profile
    
  3. Install MySQLdb

    sudo pip install MySQL-python
    

    To test if everything works fine just try

    python -c "import MySQLdb"
    

It worked like a charm for me. I hope it helps.

If you encounter an error regarding a missing library: Library not loaded: libmysqlclient.18.dylib then you have to symlink it to /usr/lib like so:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 

回答 2

通过Macports安装mysql和python 搬运工完成了所有困难的工作。

sudo port install py26-mysql 
sudo port install mysql5-server

应该安装您需要的东西。(请参阅堆栈溢出以获取有关mysql服务器的注释

如果只需要连接到mysql而不运行服务器,那么第一行就足够了。

Macports现在(2013年初)将为操作系统的常见组合和可执行体系结构提供二进制下载,而对于其他操作系统(如果需要),它将从源代码构建。

通常,当有复杂的库等需要安装时,macports(或fink)会提供帮助。

仅使用Python的代码,如果可以通过setuptools等设置简单的C依赖关系,但是如果将两者混合使用,它就会变得复杂起来。

Install mysql and python via Macports The porters have done all the difficult work.

sudo port install py26-mysql 
sudo port install mysql5-server

should install what you need. (see Stack overflow for comments re mysql server)

If you only need to connect to mysql and not run a server then the first line is sufficient.

Macports now (early 2013) will provide binary downloads for common combinations of OS a executable architecture, for others (and if you request it) it will build from source.

In general macports (or fink) help when there are complex libraries etc that need to be installed.

Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.


回答 3

安装点子:

sudo easy_install pip

安装brew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

安装mysql:

brew install mysql

安装MySQLdb

sudo pip install MySQL-python

如果您遇到编译问题,请尝试编辑〜/ .profile文件,如此处的答案之一所示。

Install pip:

sudo easy_install pip

Install brew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install mysql:

brew install mysql

Install MySQLdb

sudo pip install MySQL-python

If you have compilation problems, try editing the ~/.profile file like in one of the answers here.


回答 4

在完成第9步时收到错误消息后,我还必须完成另一个步骤:

ImportError: dlopen(/Users/rick/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

参考:谢谢!http://ageekstory.blogspot.com/2011_04_01_archive.html

Here’s another step I had to go through, after receiving an error on completing Step 9:

ImportError: dlopen(/Users/rick/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Reference: Thanks! http://ageekstory.blogspot.com/2011_04_01_archive.html


回答 5

在Mac安装MySQL-python所述

pip uninstall MySQL-python
brew install mysql
pip install MySQL-python

然后测试一下:

python -c "import MySQLdb"

As stated on Installing MySQL-python on mac :

pip uninstall MySQL-python
brew install mysql
pip install MySQL-python

Then test it :

python -c "import MySQLdb"

回答 6

刚得到一个新的Lion盒之后,又遇到了这个问题(再次!)。

我发现的最佳解决方案(仍然不是100%最佳,但可以工作):

可以通过从Apple下载XCode / Dev工具获得它-这是一个很大的下载-

…但是我推荐这个github,它具有您所需要的(并且没有XCode):https : //github.com/kennethreitz/osx-gcc-installer

我下载了他们为狮子预装的PKG,https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

  • 确保您已下载64位版本的MYSQL社区。(DMG安装是一条简单的路线)http://dev.mysql.com/downloads/mysql/

  • 设置路径如下:

    导出PATH = $ PATH:/ usr / local / mysql-XXXX

    导出DYLD_LIBRARY_PATH = / usr / local / mysql / lib /

    出口ARCHFLAGS =’-arch x86_64′

注意:

在上面的mysql-XXXX中为1,XXX是您下载的特定版本。(也许/ usr / local / mysql /也可以工作,因为这很可能是相同的别名,但是我不会假装知道您的设置)

2我已经看到它建议将ARCHFLAGS设置为’-arch i386 -arch x86_64’,但是仅指定x86_64似乎对我来说更好。(我可以想到一些原因,但是它们并不严格相关)。

  • 安装野兽!

    easy_install MySQL-python

  • 最后一步:

永久添加DYLD_LIBRARY_PATH!

您可以将其添加到您的bash_profile或类似文件中。这是我所缺少的步骤,没有它,我的系统将继续坚持各种错误来查找_mysql.so等。

导出DYLD_LIBRARY_PATH = / usr / local / mysql / lib /

@ richard-boardman,刚注意到您的软链接解决方案,实际上可能是在执行我的PATH解决方案所做的相同工作……民间,无论哪种方法最适合您。

最佳参考:http : //activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/

Just had this problem (again!) after getting a new Lion box.

Best solution I’ve found (still not 100% optimal, but working):

you can get it by downloading XCode/Dev Tools from Apple – this is a big download –

… but instead I recommend this github which has what you need (and does not have XCode): https://github.com/kennethreitz/osx-gcc-installer

I downloaded their prebuilt PKG for lion, https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

  • make sure you have downloaded a 64-BIT version of MYSQL Community. (The DMG install is an easy route) http://dev.mysql.com/downloads/mysql/

  • Set paths as follows:

    export PATH=$PATH:/usr/local/mysql-XXXX

    export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/

    export ARCHFLAGS=’-arch x86_64′

NOTE THAT:

1 in mysql-XXXX above, XXX is the specific version you downloaded. (Probably /usr/local/mysql/ would also work since this is most likely an alias to the same, but I won’t pretend to know your setup)

2 I have seen it suggested that ARCHFLAGS be set to ‘-arch i386 -arch x86_64’ but specifying only x86_64 seemed to work better for me. (I can think of some reasons for this but they are not strictly relevant).

  • Install the beast!

    easy_install MySQL-python

  • LAST STEP:

Permanently add the DYLD_LIBRARY_PATH!

You can add it to your bash_profile or similar. This was the missing step for me, without which my system continued to insist on various errors finding _mysql.so and so on.

export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/

@richard-boardman, just noticed your soft link solution, which may in effect be doing the same thing my PATH solution does…folks, whatever works best for you.

Best reference: http://activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/


回答 7

您可以尝试使用pure-python pymysql

sudo easy_install pymysql

(或者使用pip是否已安装。)然后,import MySQLdb在代码中添加以下代码:

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

You could try using the pure-python pymysql:

sudo easy_install pymysql

(Or use pip if you have it installed.) Then, add this before you import MySQLdb in your code:

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

回答 8

或简单尝试:

> sudo easy_install MySQL-python

如果出现如下错误:

EnvironmentError:找不到mysql_config

,然后运行

> export PATH=$PATH:/usr/local/mysql/bin

Or simple try:

> sudo easy_install MySQL-python

If it gives a error like below:

EnvironmentError: mysql_config not found

, then just run this

> export PATH=$PATH:/usr/local/mysql/bin

回答 9

如果您使用的是64位MySQL,则在构建mysql-python库时使用ARCHFLAGS指定您的cpu体系结构将达到目的:

ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install

If you are using 64 bit MySQL, using ARCHFLAGS to specify your cpu architecture while building mysql-python libraries would do the trick:

ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install

回答 10

export PATH=$PATH:/usr/local/mysql/bin/

应该会为您解决该问题,因为系统无法找到mysql_config文件。

export PATH=$PATH:/usr/local/mysql/bin/

should fix the issue for you as the system is not able to find the mysql_config file.


回答 11

在macos Sierra上,这对我有用,其中Python由anaconda管理:

anaconda search -t conda mysql-python

anaconda show CEFCA/mysql-python

conda install --channel https://conda.anaconda.org/CEFCA mysql-python

与SQLAlchemy一起使用:

Python 2.7.13 | Continuum Analytics,Inc. | (默认值,2016年12月20日,23:05:08)darwin上的[GCC 4.2.1兼容Apple LLVM 6.0(clang-600.0.57)]有关更多信息,请键入“ help”,“ copyright”,“ credits”或“ license”信息。Anaconda由Continuum Analytics带给您。请检出:http : //continuum.io/thankshttps://anaconda.org

>>>从sqlalchemy导入*

>>> dbengine = create_engine(’mysql:// ….’)

On macos Sierra this work for me, where python is managed by anaconda:

anaconda search -t conda mysql-python

anaconda show CEFCA/mysql-python

conda install --channel https://conda.anaconda.org/CEFCA mysql-python

The to use with SQLAlchemy:

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org

>>> from sqlalchemy import *

>>>dbengine = create_engine(‘mysql://….’)


回答 12

这个答案是大约在2019年11月的更新。没有一个流行的pip安装程序可以在MacOS 10.13(以及其他版本)上为您提供有效的安装程序。这是使工作正常的一种简单方法:

brew install mysql
pip install mysqlclient

如果需要安装brew的帮助,请访问以下站点:https : //brew.sh/

This answer is an update, circa November 2019. None of the popular pip installs will give you a working setup on MacOS 10.13 (and likely other versions as well). Here is a simple way that I got things working:

brew install mysql
pip install mysqlclient

If you need help installing brew, see this site: https://brew.sh/


回答 13

我遇到了这个问题,发现mysqlclient需要知道在哪里可以找到openssl,而OSX默认将其隐藏。

使用找到“ openssl” brew info openssl,然后将opensslbin 的路径添加到PATH:

export PATH="/usr/local/opt/openssl/bin:$PATH"

我建议将其添加到您的.zshrc或.bashrc中,这样以后就不必担心了。然后,导出该变量(可能需要关闭并重新打开bash会话),再添加两个env变量:

# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

然后,最后运行:

pipenv install mysqlclient

它应该安装得很好。

来源:https//medium.com/@shandou/pipenv-install-mysqlclient-on-macosx-7c253b0112f2

I ran into this issue and found that mysqlclient needs to know where to find openssl, and OSX hides this by default.

Locate openssl with brew info openssl, and then add the path to your openssl bin to your PATH:

export PATH="/usr/local/opt/openssl/bin:$PATH"

I recommend adding that to your .zshrc or .bashrc so you don’t need to worry about it in the future. Then, with that variable exported (which may meed closing and re-opening your bash session), add two more env variables:

# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then, finally, run:

pipenv install mysqlclient

and it should install just fine.

Source: https://medium.com/@shandou/pipenv-install-mysqlclient-on-macosx-7c253b0112f2


如何可靠地猜测MacRoman,CP1252,Latin1,UTF-8和ASCII之间的编码

问题:如何可靠地猜测MacRoman,CP1252,Latin1,UTF-8和ASCII之间的编码

在工作中,似乎没有一周没有编码相关的混乱,灾难或灾难。问题通常来自程序员,他们认为他们无需指定编码就可以可靠地处理“文本”文件。但是你不能。

因此,已决定从此以后禁止文件以*.txt或结尾的文件名*.text。这种想法是,这些扩展误导了随意的程序员对编码的沉闷自满,这会导致处理不当。根本没有扩展将是更好的选择,因为至少您知道自己不知道拥有什么。

但是,我们不会走那么远。相反,您将期望使用以编码结尾的文件名。因此,对于文本文件,例如,这些会是这样README.asciiREADME.latin1README.utf8,等。

对于需要特定扩展名的文件,如果可以在文件本身内部指定编码,例如在Perl或Python中,则应这样做。对于Java源之类的文件,其中文件内部没有这样的功能,您可以将编码放在扩展名之前,例如SomeClass-utf8.java

对于输出,强烈建议使用 UTF-8 。

但是作为输入,我们需要弄清楚如何处理代码库中名为的数千个文件*.txt。我们想重命名所有这些以适应我们的新标准。但是我们不可能全神贯注。因此,我们需要一个实际起作用的库或程序。

这些格式有ASCII,ISO-8859-1,UTF-8,Microsoft CP1252或Apple MacRoman。尽管我们知道我们可以判断某些东西是否为ASCII,并且知道有某种东西可能是UTF-8还是一个不错的选择,但我们对8位编码感到困惑。因为我们在大多数台式机为Mac的混合Unix环境(Solaris,Linux,Darwin)中运行,所以我们有很多烦人的MacRoman文件。这些尤其是一个问题。

一段时间以来,我一直在寻找一种以编程方式确定

  1. ASCII码
  2. ISO-8859-1
  3. CP1252
  4. 麦克罗曼
  5. UTF-8

文件在其中,我还没有找到可以可靠地区分这三种不同的8位编码的程序或库。我们可能仅拥有一千多个MacRoman文件,因此我们使用的任何字符集检测器都必须能够将它们嗅出。我看过的东西都无法解决这个问题。我对ICU字符集检测器库寄予厚望,但它不能处理MacRoman。我也研究过模块,它们在Perl和Python中都可以做同样的事情,但是一遍又一遍地是同一回事:不支持检测MacRoman。

因此,我要寻找的是一个现有的库或程序,该库或程序可以可靠地确定文件所用的五种编码中的哪一种(最好是更多)。特别是它必须区分我引用的三种3位编码,尤其是MacRoman。文件是超过99%的英语文本;还有其他几种语言,但不是很多。

如果是库代码,则我们的语言偏好是按Perl,C,Java或Python的顺序排列。如果它只是一个程序,那么我们并不在乎它的语言是什么,只要它是完整的源代码,在Unix上运行并且完全不受限制即可。

还有其他人遇到过随机编码成千上万个旧文本文件的问题吗?如果是这样,您是如何尝试解决它的?您的成功程度如何?这是我的问题中最重要的方面,但是我也很感兴趣您是否鼓励程序员使用文件中的实际编码来命名(或重命名)他们的文件,这将有助于我们将来避免此问题。有没有人曾经尝试过在制度基础上强制执行,如果成功,那么成功与否,为什么?

是的,我完全理解,考虑到问题的性质,为什么不能保证给出确切的答案。对于小文件,尤其是这种情况,因为您没有足够的数据继续运行。幸运的是,我们的文件很少。除了随机README文件外,大多数文件的大小在50k到250k之间,许多文件更大。大小超过K的任何内容都将保证使用英语。

问题领域是生物医学文本挖掘,因此我们有时会处理大量的超大型语料库,例如PubMedCentral的所有Open Access存储库。一个相当大的文件是BioThesaurus 6.0,容量为5.7 GB。该文件特别令人讨厌,因为它几乎都是UTF-8。但是,我相信有些numbskull会以一些8位编码插入其中的几行,即Microsoft CP1252。您需要花费相当长的时间才能踏上那个旅程。:(

At work it seems like no week ever passes without some encoding-related conniption, calamity, or catastrophe. The problem usually derives from programmers who think they can reliably process a “text” file without specifying the encoding. But you can’t.

So it’s been decided to henceforth forbid files from ever having names that end in *.txt or *.text. The thinking is that those extensions mislead the casual programmer into a dull complacency regarding encodings, and this leads to improper handling. It would almost be better to have no extension at all, because at least then you know that you don’t know what you’ve got.

However, we aren’t goint to go that far. Instead you will be expected to use a filename that ends in the encoding. So for text files, for example, these would be something like README.ascii, README.latin1, README.utf8, etc.

For files that demand a particular extension, if one can specify the encoding inside the file itself, such as in Perl or Python, then you shall do that. For files like Java source where no such facility exists internal to the file, you will put the encoding before the extension, such as SomeClass-utf8.java.

For output, UTF-8 is to be strongly preferred.

But for input, we need to figure out how to deal with the thousands of files in our codebase named *.txt. We want to rename all of them to fit into our new standard. But we can’t possibly eyeball them all. So we need a library or program that actually works.

These are variously in ASCII, ISO-8859-1, UTF-8, Microsoft CP1252, or Apple MacRoman. Although we’re know we can tell if something is ASCII, and we stand a good change of knowing if something is probably UTF-8, we’re stumped about the 8-bit encodings. Because we’re running in a mixed Unix environment (Solaris, Linux, Darwin) with most desktops being Macs, we have quite a few annoying MacRoman files. And these especially are a problem.

For some time now I’ve been looking for a way to programmatically determine which of

  1. ASCII
  2. ISO-8859-1
  3. CP1252
  4. MacRoman
  5. UTF-8

a file is in, and I haven’t found a program or library that can reliably distinguish between those the three different 8-bit encodings. We probably have over a thousand MacRoman files alone, so whatever charset detector we use has to be able to sniff those out. Nothing I’ve looked at can manage the trick. I had big hopes for the ICU charset detector library, but it cannot handle MacRoman. I’ve also looked at modules to do the same sort of thing in both Perl and Python, but again and again it’s always the same story: no support for detecting MacRoman.

What I am therefore looking for is an existing library or program that reliably determines which of those five encodings a file is in—and preferably more than that. In particular it has to distinguish between the three 3-bit encoding I’ve cited, especially MacRoman. The files are more than 99% English language text; there are a few in other languages, but not many.

If it’s library code, our language preference is for it to be in Perl, C, Java, or Python, and in that order. If it’s just a program, then we don’t really care what language it’s in so long as it comes in full source, runs on Unix, and is fully unencumbered.

Has anyone else had this problem of a zillion legacy text files randomly encoded? If so, how did you attempt to solve it, and how successful were you? This is the most important aspect of my question, but I’m also interested in whether you think encouraging programmers to name (or rename) their files with the actual encoding those files are in will help us avoid the problem in the future. Has anyone ever tried to enforce this on an institutional basis, and if so, was that successful or not, and why?

And yes, I fully understand why one cannot guarantee a definite answer given the nature of the problem. This is especially the case with small files, where you don’t have enough data to go on. Fortunately, our files are seldom small. Apart from the random README file, most are in the size range of 50k to 250k, and many are larger. Anything more than a few K in size is guaranteed to be in English.

The problem domain is biomedical text mining, so we sometimes deal with extensive and extremely large corpora, like all of PubMedCentral’s Open Access respository. A rather huge file is the BioThesaurus 6.0, at 5.7 gigabytes. This file is especially annoying because it is almost all UTF-8. However, some numbskull went and stuck a few lines in it that are in some 8-bit encoding—Microsoft CP1252, I believe. It takes quite a while before you trip on that one. :(


回答 0

首先,简单的情况:

ASCII码

如果您的数据不包含大于0x7F的字节,则为ASCII。(或者是7位ISO646编码,但是已经过时了。)

UTF-8

如果您的数据验证为UTF-8,则可以放心地假定它 UTF-8。由于UTF-8严格的验证规则,误报极为罕见。

ISO-8859-1与Windows-1252

这两种编码之间的唯一区别是ISO-8859-1具有C1控制字符,而Windows-1252具有可打印字符€,ƒ„…†‡ˆ‰Š‹ŒŽ“”•-〜™š› œžŸ。我见过很多使用大括号或破折号的文件,但是没有使用C1控制字符的文件。因此,甚至不必理会它们或ISO-8859-1,而只需检测Windows-1252。

现在只剩下一个问题了。

您如何区分MacRoman和cp1252?

这要复杂得多。

未定义的字符

Windows-1252中未使用字节0x81、0x8D,0x8F,0x90、0x9D。如果发生这种情况,则假定数据为MacRoman。

相同字符

两种编码中的字节0xA2(¢),0xA3(£),0xA9(©),0xB1(±),0xB5(µ)都相同。如果这些是唯一的非ASCII字节,那么选择MacRoman还是cp1252都没有关系。

统计方法

在您知道为UTF-8的数据中计算字符(非字节!)频率。确定最频繁的字符。然后使用此数据确定cp1252或MacRoman字符是否更常见。

例如,在我仅对100条随机英语Wikipedia文章执行的搜索中,最常见的非ASCII字符为·•–é°®’èö—。基于这个事实,

  • 字节0x92、0x95、0x96、0x97、0xAE,0xB0、0xB7、0xE8、0xE9或0xF6表示Windows-1252。
  • 字节0x8E,0x8F,0x9A,0xA1、0xA5、0xA8、0xD0、0xD1、0xD5或0xE1表示MacRoman。

计算cp1252建议字节和MacRoman建议字节,并选择最大的一个。

First, the easy cases:

ASCII

If your data contains no bytes above 0x7F, then it’s ASCII. (Or a 7-bit ISO646 encoding, but those are very obsolete.)

UTF-8

If your data validates as UTF-8, then you can safely assume it is UTF-8. Due to UTF-8’s strict validation rules, false positives are extremely rare.

ISO-8859-1 vs. windows-1252

The only difference between these two encodings is that ISO-8859-1 has the C1 control characters where windows-1252 has the printable characters €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ. I’ve seen plenty of files that use curly quotes or dashes, but none that use C1 control characters. So don’t even bother with them, or ISO-8859-1, just detect windows-1252 instead.

That now leaves you with only one question.

How do you distinguish MacRoman from cp1252?

This is a lot trickier.

Undefined characters

The bytes 0x81, 0x8D, 0x8F, 0x90, 0x9D are not used in windows-1252. If they occur, then assume the data is MacRoman.

Identical characters

The bytes 0xA2 (¢), 0xA3 (£), 0xA9 (©), 0xB1 (±), 0xB5 (µ) happen to be the same in both encodings. If these are the only non-ASCII bytes, then it doesn’t matter whether you choose MacRoman or cp1252.

Statistical approach

Count character (NOT byte!) frequencies in the data you know to be UTF-8. Determine the most frequent characters. Then use this data to determine whether the cp1252 or MacRoman characters are more common.

For example, in a search I just performed on 100 random English Wikipedia articles, the most common non-ASCII characters are ·•–é°®’èö—. Based on this fact,

  • The bytes 0x92, 0x95, 0x96, 0x97, 0xAE, 0xB0, 0xB7, 0xE8, 0xE9, or 0xF6 suggest windows-1252.
  • The bytes 0x8E, 0x8F, 0x9A, 0xA1, 0xA5, 0xA8, 0xD0, 0xD1, 0xD5, or 0xE1 suggest MacRoman.

Count up the cp1252-suggesting bytes and the MacRoman-suggesting bytes, and go with whichever is greatest.


回答 1

Mozilla nsUniversalDetector(Perl绑定:Encode :: Detect / Encode :: Detect :: Detector)已被证明了百万倍。


回答 2

我尝试进行这种试探(假设您已经排除了ASCII和UTF-8):

  • 如果根本不显示0x7f到0x9f,则可能是ISO-8859-1,因为它们是很少使用的控制代码。
  • 如果大量出现0x91到0x94,则可能是Windows-1252,因为它们是“智能引号”,是该范围内最有可能在英文文本中使用的字符。可以肯定的是,您可以寻找对。
  • 否则,它是MacRoman,尤其是如果您看到很多0xd2到0xd5(在MacRoman中是印刷引号)。

边注:

对于像Java源这样的文件,其中文件内部没有这种功能,您可以将编码放在扩展名之前,例如SomeClass-utf8.java。

不要这样做!!

Java编译器期望文件名与类名匹配,因此重命名文件将使源代码不可编译。正确的做法是猜测编码,然后使用该native2ascii工具将所有非ASCII字符转换为Unicode转义序列

My attempt at such a heuristic (assuming that you’ve ruled out ASCII and UTF-8):

  • If 0x7f to 0x9f don’t appear at all, it’s probably ISO-8859-1, because those are very rarely used control codes.
  • If 0x91 through 0x94 appear at lot, it’s probably Windows-1252, because those are the “smart quotes”, by far the most likely characters in that range to be used in English text. To be more certain, you could look for pairs.
  • Otherwise, it’s MacRoman, especially if you see a lot of 0xd2 through 0xd5 (that’s where the typographic quotes are in MacRoman).

Side note:

For files like Java source where no such facility exists internal to the file, you will put the encoding before the extension, such as SomeClass-utf8.java

Do not do this!!

The Java compiler expects file names to match class names, so renaming the files will render the source code uncompilable. The correct thing would be to guess the encoding, then use the native2ascii tool to convert all non-ASCII characters to Unicode escape sequences.


回答 3

“ Perl,C,Java或Python,并按此顺序”:有趣的态度:-)

“我们知道一个东西是否可能是UTF-8,这是一个很好的改变”:实际上,当UTF-8很小时,包含以其他字符集编码的,使用高位字节的有意义文本的文件将成功解码的机会。

UTF-8策略(至少使用首选语言):

# 100% Unicode-standard-compliant UTF-8
def utf8_strict(text):
    try:
        text.decode('utf8')
        return True
    except UnicodeDecodeError:
        return False

# looking for almost all UTF-8 with some junk
def utf8_replace(text):
    utext = text.decode('utf8', 'replace')
    dodgy_count = utext.count(u'\uFFFD') 
    return dodgy_count, utext
    # further action depends on how large dodgy_count / float(len(utext)) is

# checking for UTF-8 structure but non-compliant
# e.g. encoded surrogates, not minimal length, more than 4 bytes:
# Can be done with a regex, if you need it

一旦确定它既不是ASCII也不是UTF-8:

我知道的Mozilla起源字符集检测器不支持MacRoman,而且无论如何在8位字符集上都做得不好,尤其是对于英语,因为AFAICT依赖于检查给定解码是否有意义语言,忽略标点符号,并基于该语言的大量文档。

正如其他人所说的,您实际上只有高位标点符号可用于区分cp1252和macroman。我建议您在自己的文档上训练Mozilla类型的模型,而不是莎士比亚,《议事录》或《圣经》,并考虑所有256个字节。我认为您的文件中没有标记(HTML,XML等),这会使某些令人震惊的概率失真。

您提到的文件大多为UTF-8,但无法解码。您还应该非常怀疑:

(1)据称是用ISO-8859-1编码的文件,但包含范围在0x80至0x9F(包括0x80至0x9F)内的“控制字符” …这太普遍了,以至于HTML5标准草案表示要解码所有声明为ISO-8859的HTML流-1使用cp1252。

(2)将OK解码为UTF-8的文件,但所得的Unicode包含范围在U + 0080至U + 009F(含)范围内的“控制字符” …这可能是由于对cp1252 / cp850进行代码转换(见它发生了!)/等等文件从“ ISO-8859-1”到UTF-8。

背景:我有一个星期天下午下午的项目,以创建一个基于Python的字符集检测器,该检测器面向文件(而不是面向Web),并且可以与8位字符集(包括legacy ** ncp850和cp437等)一起很好地工作。现在还远没有黄金时间。我对培训文件感兴趣;您的ISO-8859-1 / cp1252 / MacRoman文件是否像您期望任何人的代码解决方案一样“不受阻碍”?

“Perl, C, Java, or Python, and in that order”: interesting attitude :-)

“we stand a good change of knowing if something is probably UTF-8”: Actually the chance that a file containing meaningful text encoded in some other charset that uses high-bit-set bytes will decode successfully as UTF-8 is vanishingly small.

UTF-8 strategies (in least preferred language):

# 100% Unicode-standard-compliant UTF-8
def utf8_strict(text):
    try:
        text.decode('utf8')
        return True
    except UnicodeDecodeError:
        return False

# looking for almost all UTF-8 with some junk
def utf8_replace(text):
    utext = text.decode('utf8', 'replace')
    dodgy_count = utext.count(u'\uFFFD') 
    return dodgy_count, utext
    # further action depends on how large dodgy_count / float(len(utext)) is

# checking for UTF-8 structure but non-compliant
# e.g. encoded surrogates, not minimal length, more than 4 bytes:
# Can be done with a regex, if you need it

Once you’ve decided that it’s neither ASCII nor UTF-8:

The Mozilla-origin charset detectors that I’m aware of don’t support MacRoman and in any case don’t do a good job on 8-bit charsets especially with English because AFAICT they depend on checking whether the decoding makes sense in the given language, ignoring the punctuation characters, and based on a wide selection of documents in that language.

As others have remarked, you really only have the high-bit-set punctuation characters available to distinguish between cp1252 and macroman. I’d suggest training a Mozilla-type model on your own documents, not Shakespeare or Hansard or the KJV Bible, and taking all 256 bytes into account. I presume that your files have no markup (HTML, XML, etc) in them — that would distort the probabilities something shocking.

You’ve mentioned files that are mostly UTF-8 but fail to decode. You should also be very suspicious of:

(1) files that are allegedly encoded in ISO-8859-1 but contain “control characters” in the range 0x80 to 0x9F inclusive … this is so prevalent that the draft HTML5 standard says to decode ALL HTML streams declared as ISO-8859-1 using cp1252.

(2) files that decode OK as UTF-8 but the resultant Unicode contains “control characters” in the range U+0080 to U+009F inclusive … this can result from transcoding cp1252 / cp850 (seen it happen!) / etc files from “ISO-8859-1” to UTF-8.

Background: I have a wet-Sunday-afternoon project to create a Python-based charset detector that’s file-oriented (instead of web-oriented) and works well with 8-bit character sets including legacy ** n ones like cp850 and cp437. It’s nowhere near prime time yet. I’m interested in training files; are your ISO-8859-1 / cp1252 / MacRoman files as equally “unencumbered” as you expect anyone’s code solution to be?


回答 4

您已经发现,没有完美的方法来解决此问题,因为如果没有关于文件使用哪种编码的隐式知识,所有8位编码都是完全相同的:字节的集合。所有字节对于所有8位编码均有效。

您可以期望的最好结果是某种算法,可以分析字节,并基于以某种语言以某种编码使用某种字节的概率,可以猜测文件使用的编码方式。但这必须知道文件使用哪种语言,并且当您使用混合编码的文件时,它变得完全无用。

从好的方面来说,如果您知道文件中的文本是用英语编写的,那么您决定使用该文件的任何编码都不会引起任何差异,因为所有提到的编码之间的差异都本地化在编码的一部分,指定了英语中通常不使用的字符。在文本使用特殊格式或特殊版本的标点符号(例如CP1252具有引号字符的多个版本)的情况下,您可能会遇到一些麻烦,但是对于文本的要旨而言,可能没有任何问题。

As you have discovered, there is no perfect way to solve this problem, because without the implicit knowledge about which encoding a file uses, all 8-bit encodings are exactly the same: A collection of bytes. All bytes are valid for all 8-bit encodings.

The best you can hope for, is some sort of algorithm that analyzes the bytes, and based on probabilities of a certain byte being used in a certain language with a certain encoding will guess at what encoding the files uses. But that has to know which language the file uses, and becomes completely useless when you have files with mixed encodings.

On the upside, if you know that the text in a file is written in English, then the you’re unlikely to notice any difference whichever encoding you decide to use for that file, as the differences between all the mentioned encodings are all localized in the parts of the encodings that specify characters not normally used in the English language. You might have some troubles where the text uses special formatting, or special versions of punctuation (CP1252 has several versions of the quote characters for instance), but for the gist of the text there will probably be no problems.


回答 5

如果您可以检测到除宏人以外的所有编码,那么逻辑上是假设无法解密的是宏人。换句话说,只要列出无法处理的文件,然后将其视为宏文件即可。

排序这些文件的另一种方法是制作一个基于服务器的程序,该程序允许用户确定哪种编码不乱码。当然,这将在公司内部,但是如果有100名员工每天做几次工作,那么您将立即拥有成千上万的文件。

最后,将所有现有文件转换为单一格式并要求新文件采用该格式不是更好。

If you can detect every encoding EXCEPT for macroman, than it would be logical to assume that the ones that can’t be deciphered are in macroman. In other words, just make a list of files that couldn’t be processed and handle those as if they were macroman.

Another way to sort these files would be to make a server based program that allows users to decide which encoding isn’t garbled. Of course, it would be within the company, but with 100 employees doing a few each day, you’ll have thousands of files done in no time.

Finally, wouldn’t it be better to just convert all existing files to a single format, and require that new files be in that format.


回答 6

还有其他人遇到过随机编码成千上万个旧文本文件的问题吗?如果是这样,您是如何尝试解决它的?您的成功程度如何?

我目前正在编写将文件转换为XML的程序。它必须自动检测每个文件的类型,这是确定文本文件编码问题的超集。为了确定编码,我使用贝叶斯方法。也就是说,我的分类代码针对文本文件能够理解的所有编码,计算出文本文件具有特定编码的概率(可能性)。然后,程序选择最可能的解码器。对于每种编码,贝叶斯方法都像这样工作。

  1. 根据每次编码的频率,设置文件在编码中的初始(优先)概率。
  2. 依次检查文件中的每个字节。查找字节值,以确定该字节值存在与该编码中实际存在的文件之间的相关性。使用该相关性来计算新的(后验文件在编码中)概率。如果要检查的字节更多,请在检查下一个字节时将该字节的后验概率用作先验概率。
  3. 当您到达文件末尾时(我实际上仅查看前1024个字节),则具有的可能性就是文件处于编码状态的可能性。

可以看出,如果您计算信息内容而不是计算概率,而这是几率的对数,那么贝叶斯定理变得非常容易做到:info = log(p / (1.0 - p))

您将必须通过检查手动分类的文件的语料库来计算初始先验概率和相关性。

Has anyone else had this problem of a zillion legacy text files randomly encoded? If so, how did you attempt to solve it, and how successful were you?

I am currently writing a program that translates files into XML. It has to autodetect the type of each file, which is a superset of the problem of determining the encoding of a text file. For determining the encoding I am using a Bayesian approach. That is, my classification code computes a probability (likelihood) that a text file has a particular encoding for all the encodings it understands. The program then selects the most probable decoder. The Bayesian approach works like this for each encoding.

  1. Set the initial (prior) probability that the file is in the encoding, based on the frequencies of each encoding.
  2. Examine each byte in turn in the file. Look-up the byte value to determine the correlation between that byte value being present and a file actually being in that encoding. Use that correlation to compute a new (posterior) probability that the file is in the encoding. If you have more bytes to examine, use the posterior probability of that byte as the prior probability when you examine the next byte.
  3. When you get to the end of the file (I actually look at only the first 1024 bytes), the proability you have is the probability that the file is in the encoding.

It transpires that Bayes’ theorem becomes very easy to do if instead of computing probabilities, you compute information content, which is the logarithm of the odds: info = log(p / (1.0 - p)).

You will have to compute the initail priori probability, and the correlations, by examining a corpus of files that you have manually classified.