[英]What is the simplest way to make matplotlib in OSX work in a virtual environment?
I just discovered that matplotlib
has issue with virtual environments. 我刚刚发现
matplotlib
存在虚拟环境问题。 I tried the solutions in the FAQs but they didn't work. 我尝试了常见问题解答中的解决方案,但它们无效。 An ideal solution should only involve
pip
commands, but it might be hard or unrealistic to request that. 一个理想的解决方案应该只涉及
pip
命令,但要求它可能是困难的或不现实的。 Anyway, I tried what they had on the OS X section to make a framework
bash file in your venv
folder and run command through it. 无论如何,我尝试了他们在OS X部分上的内容,在你的
venv
文件夹中创建一个framework
bash文件并通过它运行命令。
$ frameworkpython krls.py
when I did that I got a permission deniel error: 当我这样做时,我得到了一个允许deniel错误:
Permission denied
I am sort of scared of sudoing commands that I am not 100% what they do...anyway, it seemed the bash script isn't doing anything too dangerous so I went ahead and sudo-ed it. 我有点害怕sudoing命令,我不是100%他们做什么...无论如何,似乎bash脚本没有做任何太危险的事情所以我继续前进并sudo-ed它。 However, the response of my terminal was weird, it said:
然而,我的终端的反应很奇怪,它说:
sudo frameworkpython krsl.py
sudo: frameworkpython: command not found
which means it doesn't recognize frameworkpython
as a command. 这意味着它不会将
frameworkpython
识别为命令。 Without the sudo it says: 没有sudo,它说:
frameworkpython krsl.py -bash: /Users/my_name/path/venv/bin/frameworkpython: Permission denied frameworkpython krsl.py -bash:/ Users / my_name / path / venv / bin / frameworkpython:权限被拒绝
which seems it recognizes frameworkpython as a command (?) but it didn't execute it due to permissions? 它似乎将frameworkpython识别为命令(?)但由于权限而没有执行它? It seems strange to me.
这对我来说似乎很奇怪。 Anyone any ideas?
任何想法?
I also tried: 我也尝试过:
$ pip install TKAgg
Collecting TKAgg
Could not find a version that satisfies the requirement TKAgg (from versions: )
No matching distribution found for TKAgg
but it didn't work. 但它不起作用。
So I tried the next option which is using PySide which also didn't work and gave a giant error output: 所以我尝试了下一个使用PySide的选项,它也没有用,并给出了一个巨大的错误输出:
$ pip install pyside
Collecting pyside
Using cached PySide-1.2.4.tar.gz
Building wheels for collected packages: pyside
Running setup.py bdist_wheel for pyside ... error
Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/tmpUR9qFCpip-wheel- --python-tag cp27:
Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
running bdist_wheel
running build
Python architecture is 64bit
error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.
----------------------------------------
Failed building wheel for pyside
Running setup.py clean for pyside
Failed to build pyside
Installing collected packages: pyside
Running setup.py install for pyside ... error
Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside:
Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
running install
running build
Python architecture is 64bit
error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.
----------------------------------------
Command "/Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside" failed with error code 1 in /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/
After that then I tried the next option using WX Phonix . 之后我尝试使用WX Phonix的下一个选项。 Unfortunately, I went through their site and couldn't find how to do it.
不幸的是,我经历了他们的网站,无法找到如何做到这一点。
Update: 更新:
With Python3, you can use the built-in implementation of virtualenv
via -m venv
: 使用Python3,您可以通过
-m venv
使用virtualenv
的内置实现:
python -m venv <name of virtualenv>
source <name of virtualenv>/bin/activate
Python3's builtin implementation builds the virtualenv such that Python is set up as a framework, so no need to configure anything to get it to work. Python3的内置实现构建了virtualenv,使得Python被设置为框架,因此不需要配置任何东西来使其工作。 More details here .
更多细节在这里 。
Original Answer: 原答案:
As stated in the FAQS you reference , you will need to create a frameworkpython
scripts in venv/bin
. 如您引用的FAQS中所述,您需要在
venv/bin
创建一个frameworkpython
脚本。 It looks like your system is unable to find frameworkpython
so it is either not on your systems $PATH
or it is not executable. 看起来您的系统无法找到
frameworkpython
因此它不在您的系统$PATH
或者它不可执行。
Make sure you make this scripts executable via chmod +x venv/bin/frameworkpython
! 确保通过
chmod +x venv/bin/frameworkpython
使这些脚本可执行!
From Linux/Unix docs (I added the bold): 从Linux / Unix文档 (我添加了粗体):
PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files.
PATH是Linux和其他类Unix操作系统中的环境变量,它告诉shell哪些目录可以搜索可执行文件。
Your system will only look for executable files accessible via the PATH
environment variable, skipping over non-executable files. 您的系统将只查找可通过
PATH
环境变量访问的可执行文件,跳过非可执行文件。
Creating the frameworkpython
script is necessary because matplotlib
requires a framework build of python. 创建
frameworkpython
脚本是必要的,因为matplotlib
需要python的框架构建。 From the link above: 从上面的链接:
Unfortunately virtualenv creates a non framework build even if created from a framework build of Python.
不幸的是,即使从Python的框架构建中创建,virtualenv也会创建非框架构建。
Here's another post with more details on framework builds 这是另一篇文章 ,详细介绍了框架构建
If you are using Python 2.x then, use these commands in virtual environment: 如果您正在使用Python 2.x,请在虚拟环境中使用以下命令:
import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt
This makes the matplotlib work in the virtual environment too. 这使得matplotlib也可以在虚拟环境中工作。
I hope this helps. 我希望这有帮助。
Much like your setup, I am using matplotlib
from within a virtual environment on OSX. 与您的设置非常相似,我在OSX上的虚拟环境中使用
matplotlib
。
I see that you have spent quite some effort to get things running and I am not certain whether or not this will actually solve your problem, since no two setups are the same. 我发现你花了相当多的精力让事情运转起来,我不确定这是否能真正解决你的问题,因为没有两个设置是相同的。 However, I drop my configuration such that you can compare and possibly find something that helps you solving this issue.
但是,我放弃了我的配置,以便您可以比较并找到可以帮助您解决此问题的内容。
Disclaimer : I use virtualenvwrapper
and have stored my venvs under ~/.virtualenvs/
. 免责声明 :我使用
virtualenvwrapper
并将我的venv存储在~/.virtualenvs/
。
Copy the frameworkpython
file to ~/.virtualenvs/YOUR_VENV/bin
将
frameworkpython
文件复制到~/.virtualenvs/YOUR_VENV/bin
Make sure to change the PATHTOPYTHON
variable within the frameworkpython
bash script to refer to the correct location. 确保在
frameworkpython
bash脚本中更改PATHTOPYTHON
变量以引用正确的位置。 For my setup, I have changed it to /usr/bin/
(while the original setting was /usr/local/bin
) 对于我的设置,我已将其更改为
/usr/bin/
(原始设置为/usr/local/bin
)
Set executable flag chmod +x frameworkpython
设置可执行标志
chmod +x frameworkpython
Install libs using pip
as usual after switching to virtual environment using virtualenvwrapper: workon YOUR_VENV
使用virtualenvwrapper切换到虚拟环境后,像往常一样使用
pip
安装lib: workon YOUR_VENV
Sourcing frameworkpython
as regular user (no root, pls!) and importing matplotlib
yields: 采用
frameworkpython
作为普通用户(没有root,请等!)并导入matplotlib
产生:
$ ./frameworkpython
Python 2.7.0 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl
>>> mpl.__version__
'1.5.1'
>>> import matplotlib.pyplot as plt
>>> ^D
Extending @Gator_Python's and @juanpa.arrivillaga's suggestions, I recommend installing the latest version of anaconda . 扩展@ Gator_Python和@ juanpa.arrivillaga的建议,我建议安装最新版本的anaconda 。 It is a cross-platform, pip-like package manager that also handles virtual environments.
它是一个跨平台,类似pip的包管理器,也可以处理虚拟环境。 It also installs large packages like
numpy
, pandas
and matplotlib
with little headache. 它还安装了像
numpy
, pandas
和matplotlib
这样的大型软件包。
Setup : From the commandline, just create a virtual environment and install from there. 设置 :从命令行,只需创建一个虚拟环境并从那里安装。 Here is an example of how to set up conda (substitute the appropriate environment name and python version number respectively):
以下是如何设置conda的示例(分别替换相应的环境名称和python版本号):
conda update conda
conda update anaconda
conda create -n <envname> python=<version> matplotlib
source activate <envname>
conda info --envs # show current env
conda list # see installed env packages
This should place you in an isolated, clean environment. 这应该让您置身于一个孤立,干净的环境中。 You can see a minimal list of packages installed in this environment.
您可以在此环境中看到安装的最小程序包列表。 Make sure
pip
and setuptools
are listed (they are installed automatically). 确保列出了
pip
和setuptools
(它们是自动安装的)。 For any other packages you need, use either conda install <package>
or pip install <package>
, in that order. 对于您需要的任何其他软件包,请按顺序使用
conda install <package>
或pip install <package>
。 Install your project: 安装你的项目:
cd <path\to\project>
python setup.py develop
Removal : When you are ready to remove your environment: 删除 :准备好删除环境时:
source deactivate
conda env remove -n <envname>
Note : alternatives to setup.py develop
are: 注意 :
setup.py develop
替代方案是:
To get pyside working: 让pyside工作:
brew install cmake
brew is a pretty standard OSX dependency. brew是一个非常标准的OSX依赖。
You are right in identifying that this is a framework issue Differences between Framework and non-Framework builds of Python on Mac OS X To get this running, you will need the framework python running. 你是对的,确定这是一个框架问题Mac OS X上的Python和非框架构建之间的差异为了让这个运行,你需要运行框架python。
And even more correct in not running sudo commands w/o understanding them. 甚至更正确的是不运行sudo命令而不理解它们。 Why you might be able to see it, but not be able to run it could be due to not having the environment established correctly as sudo.
为什么你可以看到它,但无法运行它可能是因为没有像sudo那样正确建立环境。 How to keep Environment Variables when Using SUDO
如何在使用SUDO时保持环境变量
if you run which frameworkpython
it should show you the path to the executable on the file system. 如果你运行
which frameworkpython
它应该显示文件系统上可执行文件的路径。 I would check the file permissions on this file, in particular check the ownership. 我会检查此文件的文件权限,特别是检查所有权。
ls -l $(which frameworkpython)
The ouptut of which frameworkpython
should be the path that could be executed with root, but I would avoid it, as any other dependancies might not be loaded correctly, or can have the wrong permissions set, it might work now, but will get you in trouble later on. which frameworkpython
python应该是可以用root执行的路径,但我会避免它,因为任何其他依赖项可能无法正确加载,或者可能设置了错误的权限,它可能现在可以正常工作,但会让你进入麻烦以后。
/path/to/frameworkpython krls.py
When I ran into this issue this blog was an awesome resource. 当我遇到这个问题时, 这个博客是一个很棒的资源。
sudo apt-get install tk-dev libpng-dev libffi-dev dvipng texlive-latex-base
Then uninstalled and reinstalled matplotlib: 然后卸载并重新安装matplotlib:
pip uninstall matplotlib
pip install matplotlib
To echo Pedro Cattori's answer, the easiest way that has worked every time for me is to simply use python 3 with a virtual env as in: 为了回应Pedro Cattori的回答,每次对我来说最简单的方法就是简单地使用带有虚拟环境的python 3:
python -m venv my-virtualenv
source my-virtualenv/bin/activate
or 要么
python3 -m venv my-virtualenv
source my-virtualenv/bin/activate
from original FAQ page: http://matplotlib.org/faq/osx_framework.html#osxframework-faq 来自原始FAQ页面: http : //matplotlib.org/faq/osx_framework.html#osxframework-faq
Everything else I've tried has resulted in ridiculous amount of code and time spent figuring out whats wrong. 我尝试的其他所有内容都导致了大量的代码和花费时间来弄清楚什么是错误的。 With that it always works every time without me further doing much more beyond pip installing matplotlib.
有了它,它总是有效,没有我进一步做更多的pip安装matplotlib。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.