您不应该需要直接修改路径,无论是通过环境变量还是sys.path。无论您使用的是os(例如apt-get),还是virtualenv中的pip,包都将安装到路径上已经存在的位置。
In your example, GNU Radio is installed to the system Python 2's standard site-packages location, which is already in the path. Pointing PyCharm at the correct interpreter is enough; if it isn't there is something else wrong that isn't apparent. It may be that /usr/bin/python does not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7 binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restart would tell it to rescan.
这个答案将涵盖如何设置项目环境,在不同的场景中安装包,以及配置PyCharm。我多次参考Python打包用户指南,它是由维护官方Python打包工具的同一组编写的。
开发Python应用程序的正确方法是使用virtualenv。软件包和版本的安装不影响系统和其他项目。PyCharm有一个内置的界面来创建virtualenv和安装包。或者您可以从命令行创建它,然后将PyCharm指向它。
$ cd MyProject
$ python2 -m virtualenv env
$ . env/bin/activate
$ pip install -U pip setuptools # get the latest versions
$ pip install flask # install other packages
在PyCharm项目中,转到文件>设置>项目>项目解释器。如果您使用virtualenvwrapper或PyCharm来创建env,那么它应该显示在菜单中。如果不是,单击齿轮,选择添加本地,并在env. exe文件中找到Python二进制文件。PyCharm将显示所选环境中的所有包。
在某些情况下,例如在GNU Radio中,pip中没有安装包,当您安装其余的GNU Radio时,该包在系统范围内安装(例如apt-get install gnuradio)。在这种情况下,您仍然应该使用virtualenv,但是需要让它知道这个系统包。
$ python2 -m virtualenv --system-site-packages env
不幸的是,它看起来有点乱,因为所有的系统包现在都将出现在你的环境中,但它们只是链接,你仍然可以安全地安装或升级包而不影响系统。
在某些情况下,您将有多个正在开发的本地包,并且希望一个项目使用另一个包。在这种情况下,您可能认为必须将本地包添加到其他项目的路径中,但事实并非如此。您应该在开发模式下安装包。这只需要向包中添加一个setup.py文件,为了以后正确地分发和部署包,无论如何都需要这个文件。
你的第一个项目的最小setup.py:
from setuptools import setup, find_packages
setup(
name='mypackage',
version='0.1',
packages=find_packages(),
)
然后将它安装到第二个项目的env中:
$ pip install -e /path/to/first/project