标签归档:macports

在Mac上安装python模块的最兼容方法是什么?

问题:在Mac上安装python模块的最兼容方法是什么?

我开始学习python并热爱它。我主要在Mac和Linux上工作。我发现在Linux(主要是Ubuntu 9.04)上,使用apt-get安装python模块时,它工作正常。我可以轻松导入它。

在Mac上,我习惯于使用Macports安装所有Unixy东西。但是,我发现我安装的大多数python模块都没有被python看到。我花了一些时间玩弄PATH设置并使用python_select。什么都没有真正起作用,在这一点上我还不是很了解,相反,我只是闲逛。

我得到的印象是Macports在管理python模块方面并不广受喜爱。我想使用更“接受”(如果正确的话)的方法重新开始。

因此,我想知道Mac python开发人员使用什么方法来管理其模块?

奖励问题:

您使用Apple的python还是其他版本?您是从源代码编译所有内容,还是有运行良好的软件包管理器(Fink?)。

I’m starting to learn python and loving it. I work on a Mac mainly as well as Linux. I’m finding that on Linux (Ubuntu 9.04 mostly) when I install a python module using apt-get it works fine. I can import it with no trouble.

On the Mac, I’m used to using Macports to install all the Unixy stuff. However, I’m finding that most of the python modules I install with it are not being seen by python. I’ve spent some time playing around with PATH settings and using python_select . Nothing has really worked and at this point I’m not really understanding, instead I’m just poking around.

I get the impression that Macports isn’t universally loved for managing python modules. I’d like to start fresh using a more “accepted” (if that’s the right word) approach.

So, I was wondering, what is the method that Mac python developers use to manage their modules?

Bonus questions:

Do you use Apple’s python, or some other version? Do you compile everything from source or is there a package manger that works well (Fink?).


回答 0

管理python软件包的最流行方法(如果您不使用系统软件包管理器)是使用setuptools和easy_install。它可能已经安装在您的系统上。像这样使用它:

easy_install django

easy_install使用Python软件包索引 ,这对于python开发人员来说是一个了不起的资源。环顾四周,看看有哪些软件包可用。

更好的选择是pip,它越来越受欢迎,因为它试图解决与easy_install相关的许多问题。Pip使用与easy_install相同的软件包存储库,但效果更好。真正唯一需要使用easy_install的时间是此命令:

easy_install pip

之后,使用:

pip install django

在某个时候,您可能需要学习有关virtualenv的知识。如果您在软件包需求冲突的项目上进行大量的python开发,那么virtualenv是天赐之物。它将允许您使用各种软件包的完全不同的版本,并根据需要在它们之间轻松切换。

关于使用哪个python,坚持使用Apple的python会让您头疼最少,但是如果您需要更新的版本(我相信Leopard是2.5.1),我会选择macports python 2.6。

The most popular way to manage python packages (if you’re not using your system package manager) is to use setuptools and easy_install. It is probably already installed on your system. Use it like this:

easy_install django

easy_install uses the Python Package Index which is an amazing resource for python developers. Have a look around to see what packages are available.

A better option is pip, which is gaining traction, as it attempts to fix a lot of the problems associated with easy_install. Pip uses the same package repository as easy_install, it just works better. Really the only time use need to use easy_install is for this command:

easy_install pip

After that, use:

pip install django

At some point you will probably want to learn a bit about virtualenv. If you do a lot of python development on projects with conflicting package requirements, virtualenv is a godsend. It will allow you to have completely different versions of various packages, and switch between them easily depending your needs.

Regarding which python to use, sticking with Apple’s python will give you the least headaches, but If you need a newer version (Leopard is 2.5.1 I believe), I would go with the macports python 2.6.


回答 1

您的问题已经三岁了,有些其他答案未涵盖的细节:

我认识的大多数人都使用HomeBrewMacPorts,因此我更喜欢MacPorts,因为它清晰地默认了Mac OS X环境和我的开发设置。只需移出/ opt文件夹并使用普通用户Python环境测试您的软件包即可

MacPorts仅可在Mac中移植,但是通过easy_install或pip,您将学习如何在任何平台(Win / Mac / Linux / Bsd …)上设置环境。此外,它将始终是最新的,并且具有更多的软件包

我个人让MacPorts处理我的Python模块以保持所有更新。像任何其他高级软件包管理器(即apt-get)一样,对于具有大量二进制相关性的繁重模块而言,这要好得多。我无法用easy_install或pip构建Qt绑定(PySide)。Qt非常庞大,需要大量编译。一旦您想要一个需要非Python程序使用的库的Python包,请尝试避免easy_install或pip

在某些时候,您会发现MacPorts中缺少某些软件包。我不相信MacPorts会为您提供整个CheeseShop。例如,最近我需要Elixir模块,但是MacPorts仅提供py25-elixir和py26-elixir,没有py27版本。在这种情况下,您可以:

pip-2.7安装–user elixir

(确保您总是输入pip-(version))

这将在您的主目录中构建一个额外的Python库。是的,Python将在一个以上的库位置中工作:一个库由MacPorts控制,而一个用户本地库则用于MacPorts中缺少的所有内容。

现在注意,我更喜欢pip而不是easy_install。有充分的理由应该避免使用setuptools和easy_install。这是一个很好的解释,我尝试远离它们。pip的一项非常有用的功能是为您提供与MacPorts,easy_install和pip本身一起安装的所有模块(及其版本)的列表:

pip-2.7冻结

如果您已经开始使用easy_install,请放心,pip可以识别easy_install已经完成的所有操作,甚至可以升级随其安装的软件包。

如果您是开发人员,请密切注意virtualenv,以控制不同的设置和模块版本的组合。其他答案已经提到了它,到目前为止没有提到的是Tox模块,该工具用于测试您的软件包是否可以在不同的Python版本中正确安装。

尽管我通常没有版本冲突,但我还是喜欢virtualenv来设置一个干净的环境并清晰地查看我的软件包依赖项。这样,我就永远不会忘记setup.py中的任何依赖项

如果您选择MacPorts,请注意,不再选择同一软件包的多个版本,例如带有额外python_select软件包的旧Debian风格(出于兼容性,它仍然存在)。现在,您可以使用select命令选择将使用哪个Python版本(甚至可以选择Apple安装的版本):

$  port select python
Available versions for python:
    none
    python25-apple
    python26-apple
    python27 (active)
    python27-apple
    python32

$ port select python python32

在它上面添加tox,您的程序应该真正可移植

Your question is already three years old and there are some details not covered in other answers:

Most people I know use HomeBrew or MacPorts, I prefer MacPorts because of its clean cut of what is a default Mac OS X environment and my development setup. Just move out your /opt folder and test your packages with a normal user Python environment

MacPorts is only portable within Mac, but with easy_install or pip you will learn how to setup your environment in any platform (Win/Mac/Linux/Bsd…). Furthermore it will always be more up to date and with more packages

I personally let MacPorts handle my Python modules to keep everything updated. Like any other high level package manager (ie: apt-get) it is much better for the heavy lifting of modules with lots of binary dependencies. There is no way I would build my Qt bindings (PySide) with easy_install or pip. Qt is huge and takes a lot to compile. As soon as you want a Python package that needs a library used by non Python programs, try to avoid easy_install or pip

At some point you will find that there are some packages missing within MacPorts. I do not believe that MacPorts will ever give you the whole CheeseShop. For example, recently I needed the Elixir module, but MacPorts only offers py25-elixir and py26-elixir, no py27 version. In cases like these you have:

pip-2.7 install –user elixir

( make sure you always type pip-(version) )

That will build an extra Python library in your home dir. Yes, Python will work with more than one library location: one controlled by MacPorts and a user local one for everything missing within MacPorts.

Now notice that I favor pip over easy_install. There is a good reason you should avoid setuptools and easy_install. Here is a good explanation and I try to keep away from them. One very useful feature of pip is giving you a list of all the modules (along their versions) that you installed with MacPorts, easy_install and pip itself:

pip-2.7 freeze

If you already started using easy_install, don’t worry, pip can recognize everything done already by easy_install and even upgrade the packages installed with it.

If you are a developer keep an eye on virtualenv for controlling different setups and combinations of module versions. Other answers mention it already, what is not mentioned so far is the Tox module, a tool for testing that your package installs correctly with different Python versions.

Although I usually do not have version conflicts, I like to have virtualenv to set up a clean environment and get a clear view of my packages dependencies. That way I never forget any dependencies in my setup.py

If you go for MacPorts be aware that multiple versions of the same package are not selected anymore like the old Debian style with an extra python_select package (it is still there for compatibility). Now you have the select command to choose which Python version will be used (you can even select the Apple installed ones):

$  port select python
Available versions for python:
    none
    python25-apple
    python26-apple
    python27 (active)
    python27-apple
    python32

$ port select python python32

Add tox on top of it and your programs should be really portable


回答 2

请参阅Python OS X开发环境。最好的方法是使用MacPorts。下载并安装MacPorts,然后通过在终端中键入以下命令来通过MacPorts安装Python:

须藤端口安装python26 python_select
sudo端口选择--set python python26

要么

须藤端口安装python30 python_select
sudo端口选择--set python python30

使用第一组命令安装Python 2.6,使用第二组命令安装Python 3.0。然后使用:

sudo端口安装py26-packagename

要么

sudo端口安装py30-packagename

在上述命令中,将packagename替换为软件包的名称,例如:

sudo端口安装py26-setuptools

这些命令将自动为给定的Python版本安装软件包(及其依赖项)。

有关Python可用软件包的完整列表,请键入:

端口列表| grep py26-

要么

端口列表| grep py30-

您使用哪个命令取决于您选择安装的Python版本。

Please see Python OS X development environment. The best way is to use MacPorts. Download and install MacPorts, then install Python via MacPorts by typing the following commands in the Terminal:

sudo port install python26 python_select
sudo port select --set python python26

OR

sudo port install python30 python_select
sudo port select --set python python30

Use the first set of commands to install Python 2.6 and the second set to install Python 3.0. Then use:

sudo port install py26-packagename

OR

sudo port install py30-packagename

In the above commands, replace packagename with the name of the package, for example:

sudo port install py26-setuptools

These commands will automatically install the package (and its dependencies) for the given Python version.

For a full list of available packages for Python, type:

port list | grep py26-

OR

port list | grep py30-

Which command you use depends on which version of Python you chose to install.


回答 3

我使用MacPorts将Python和MacPorts跟踪的任何第三方模块/opt/local安装到中,并且将任何手动安装的模块(不在MacPorts存储库中的模块)安装到中/usr/local,这从来没有引起任何问题。我认为您可能对某些MacPorts脚本和环境变量的使用感到困惑。

MacPorts python_select用于选择“当前”版本的Python,但与模块无关。例如,这允许您使用MacPorts安装Python 2.5和Python 2.6,并在安装之间进行切换。

$PATH环境变量并没有影响Python模块被加载的内容。$PYTHONPATH是您要寻找的。$PYTHONPATH应该指向包含要加载的Python模块的目录。就我而言,我的$PYTHONPATH变量包含/usr/local/lib/python26/site-packages。如果您使用MacPorts的Python,它将为您设置其他适当的目录,因此您只需向添加其他路径$PYTHONPATH。但是同样,$PATH当Python搜索您已安装的模块时,根本不使用它。

$PATH 用于查找可执行文件,所以如果你安装的MacPorts’的Python,确保/opt/local/bin在你的$PATH

I use MacPorts to install Python and any third-party modules tracked by MacPorts into /opt/local, and I install any manually installed modules (those not in the MacPorts repository) into /usr/local, and this has never caused any problems. I think you may be confused as to the use of certain MacPorts scripts and environment variables.

MacPorts python_select is used to select the “current” version of Python, but it has nothing to do with modules. This allows you to, e.g., install both Python 2.5 and Python 2.6 using MacPorts, and switch between installs.

The $PATH environment variables does not affect what Python modules are loaded. $PYTHONPATH is what you are looking for. $PYTHONPATH should point to directories containing Python modules you want to load. In my case, my $PYTHONPATH variable contains /usr/local/lib/python26/site-packages. If you use MacPorts’ Python, it sets up the other proper directories for you, so you only need to add additional paths to $PYTHONPATH. But again, $PATH isn’t used at all when Python searches for modules you have installed.

$PATH is used to find executables, so if you install MacPorts’ Python, make sure /opt/local/bin is in your $PATH.


回答 4

使用MacPorts Python安装没有任何问题。如果要从MacPorts安装python模块,但看不到它们,则可能意味着您不调用安装的MacPorts python。在终端外壳中,您可以使用绝对路径来调用可能安装的各种Python。例如:

$ /usr/bin/python2.5         # Apple-supplied 2.5 (Leopard)
$ /opt/local/bin/python2.5   # MacPorts 2.5
$ /opt/local/bin/python2.6   # MacPorts 2.6
$ /usr/local/bin/python2.6   # python.org (MacPython) 2.6
$ /usr/local/bin/python3.1   # python.org (MacPython) 3.1

要在默认情况下获取正确的python,需要确保您的shell $ PATH设置正确,以确保首先找到正确的可执行文件。另一个解决方案是为各种python定义shell别名。

就像其他人建议的那样,也可以使用python.org(MacPython)安装。 easy_install 同样可以提供帮助,但是,由于每个Python实例可能都有自己的easy_install命令,因此请确保您调用的是right easy_install

There’s nothing wrong with using a MacPorts Python installation. If you are installing python modules from MacPorts but then not seeing them, that likely means you are not invoking the MacPorts python you installed to. In a terminal shell, you can use absolute paths to invoke the various Pythons that may be installed. For example:

$ /usr/bin/python2.5         # Apple-supplied 2.5 (Leopard)
$ /opt/local/bin/python2.5   # MacPorts 2.5
$ /opt/local/bin/python2.6   # MacPorts 2.6
$ /usr/local/bin/python2.6   # python.org (MacPython) 2.6
$ /usr/local/bin/python3.1   # python.org (MacPython) 3.1

To get the right python by default requires ensuring your shell $PATH is set properly to ensure that the right executable is found first. Another solution is to define shell aliases to the various pythons.

A python.org (MacPython) installation is fine, too, as others have suggested. easy_install can help but, again, because each Python instance may have its own easy_install command, make sure you are invoking the right easy_install.


回答 5

如果您从MacPorts使用Python,则它具有自己的easy_install,位于:/opt/local/bin/easy_install-2.6(对于py26而言)。这与简单地直接调用easy_install不同,即使您使用python_select更改了默认的python命令也是如此。

If you use Python from MacPorts, it has it’s own easy_install located at: /opt/local/bin/easy_install-2.6 (for py26, that is). It’s not the same one as simply calling easy_install directly, even if you used python_select to change your default python command.


回答 6

您有没有研究过easy_install?它不会同步您的macports或类似的东西,但是会自动下载最新的软件包和所有必要的依赖项,即

easy_install nose

用于鼻子单元测试包,或

easy_install trac

对于tracbug跟踪系统。

他们的EasyInstall页面上也有更多信息。

Have you looked into easy_install at all? It won’t synchronize your macports or anything like that, but it will automatically download the latest package and all necessary dependencies, i.e.

easy_install nose

for the nose unit testing package, or

easy_install trac

for the trac bug tracker.

There’s a bit more information on their EasyInstall page too.


回答 7

对于MacPython安装,我在此博客文章中找到了一种用于解决setuptools(easy_install)问题的有效解决方案:

http://droidism.com/getting-running-with-django-and-macpython-26-on-leopard

一个方便的技巧包括找出终端中哪个版本的python是活动的:

which python

For MacPython installations, I found an effective solution to fixing the problem with setuptools (easy_install) in this blog post:

http://droidism.com/getting-running-with-django-and-macpython-26-on-leopard

One handy tip includes finding out which version of python is active in the terminal:

which python

回答 8

使用MacPorts安装模块时,它不会进入Apple的Python版本。而是将这些模块安装到所选的MacPorts版本的Python中。

您可以使用名为python_select的mac端口更改默认使用的Python版本。这里的说明。

另外,还有easy_install。它将使用python安装python模块。

When you install modules with MacPorts, it does not go into Apple’s version of Python. Instead those modules are installed onto the MacPorts version of Python selected.

You can change which version of Python is used by default using a mac port called python_select. instructions here.

Also, there’s easy_install. Which will use python to install python modules.


回答 9

您可能已经pip3预先安装了,请尝试一下!

You may already have pip3 pre-installed, so just try it!


回答 10

关于使用哪个python版本,Mac OS通常会附带一个旧版本的python。升级到新版本是一个好主意。您可以从http://www.python.org/download/下载.dmg 。如果这样做,请记住更新路径。您可以在此处找到确切的命令http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/

Regarding which python version to use, Mac OS usually ships an old version of python. It’s a good idea to upgrade to a newer version. You can download a .dmg from http://www.python.org/download/ . If you do that, remember to update the path. You can find the exact commands here http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/


回答 11

我将easy_install与Apple的Python配合使用,它的工作原理很吸引人。

I use easy_install with Apple’s Python, and it works like a charm.


回答 12

直接安装其中一个fink软件包(2013年11月开始的Django 1.6)

fink install django-py27
fink install django-py33

或者创建一个虚拟环境:

fink install virtualenv-py27
virtualenv django-env
source django-env/bin/activate
pip install django
deactivate # when you are done

或者在virtualenv中使用fink django以及任何其他pip安装的软件包

fink install django-py27
fink install virtualenv-py27
virtualenv django-env --system-site-packages
source django-env/bin/activate
# django already installed
pip install django-analytical # or anything else you might want
deactivate # back to your normally scheduled programming

Directly install one of the fink packages (Django 1.6 as of 2013-Nov)

fink install django-py27
fink install django-py33

Or create yourself a virtualenv:

fink install virtualenv-py27
virtualenv django-env
source django-env/bin/activate
pip install django
deactivate # when you are done

Or use fink django plus any other pip installed packages in a virtualenv

fink install django-py27
fink install virtualenv-py27
virtualenv django-env --system-site-packages
source django-env/bin/activate
# django already installed
pip install django-analytical # or anything else you might want
deactivate # back to your normally scheduled programming

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