bash 在 OS Mavericks 中设置/更改 PYTHONPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24270791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:41:51  来源:igfitidea点击:

setting/changing PYTHONPATH in OS Mavericks

pythonmacosbashpathpythonpath

提问by sivanes

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which pythonin Terminal shows the /usr/bin/pythondirectory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2. "Explain like I'm five".

好的,我不想开始另一个看似数百个关于此的其他线程,但是我如何设置或调整 PYTHONPATH?我对 Mac 非常陌生,唯一一次我成功地做到了这一点是在 Windows 上使用快速环境编辑器。这看起来像一个完全不同的野兽。我尝试安装 Django,但它在 2.7 上失败了,而我需要 3.4,所以当前路径必须是 2.7。which python在终端中运行显示/usr/bin/python目录,然后我无法 cd 进入,更不用说通过浏览找到了。我看到我的 Python 3.4 目录有更新外壳配置文件,但它有很多限制。我还看到其他线程在 IDLE 中提到了 PYTHONPATH 命令,并为终端创建了 bash 配置文件类型文件之一。在我需要运行不同版本的 Python 之前,如何设置它而不用再担心它?我在 Mac 10.9.2 上。“像我五岁那样解释”。

回答by Padraic Cunningham

If you want to install packages forpython 3.4use: pip3 install django

如果要安装软件包以供python 3.4使用:pip3 install django

When installing forpython 2.7just use:pip install django

安装时python 2.7只使用:pip install django

To use python 3.4type python3in your shell.

使用python 3.4类型python3in your shell.

To see where all installations of python are use: which -a python

要查看所有 python 安装的位置: which -a python

Depending on how you installed the new versions of python you will see output like:

根据您安装新版本 python 的方式,您将看到如下输出:

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

If you wanted to use the python in /usr/local/bin/pythonyou can edit your .bashrcfile and add export path="/usr/local/bin:$path".

如果您想在其中使用 python,/usr/local/bin/python您可以编辑您的.bashrc文件并添加export path="/usr/local/bin:$path".

Save, then type source .bashrcin your shell and when you type which pythonit will show something like /usr/local/bin/python

保存,然后source .bashrc在你的 shell 中输入,当你输入时 which python它会显示类似的东西/usr/local/bin/python

Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems. You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.

不要在不同版本的python上搞太多,你最终会给自己带来很多问题。您不必更改PYTHONPATH,只需指定要使用的 python 或 pip 版本,这很可能就是您需要做的全部工作。

回答by The2ndSon

To update PYTHONPATH you can run from the terminal:

要更新 PYTHONPATH,您可以从终端运行:

    export PYTHONPATH=$PYTHONPATH:/desired/path/to/add

Then to check the updated PYTHONPATH you can run:

然后检查更新的 PYTHONPATH 你可以运行:

    echo $PYTHONPATH

I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

我不确定这是否完全回答了您的问题,但这是确保模块在导入时对 python 可见的一种方法。