添加到python路径mac os x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3387695/
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-08-18 10:47:40  来源:igfitidea点击:

Add to python path mac os x

pythonmacosaddpythonpath

提问by MacPython

I thought

我想

import sys
sys.path.append("/home/me/mydir")

is appending a dir to my pythonpath

正在将目录附加到我的 pythonpath

if I print sys.path my dir is in there.

如果我打印 sys.path 我的目录就在那里。

Then I open a new command and it is not there anymore.

然后我打开一个新命令,它不再存在了。

But somehow Python cant import modules I saved in that dir.

但不知何故,Python 无法导入我保存在该目录中的模块。

What Am I doing wrong?

我究竟做错了什么?

I read .profile or .bash_profile will do the trick.

我读过 .profile 或 .bash_profile 可以解决问题。

Do I have to add:

我是否必须添加:

PATH="/Me//Documents/mydir:$PYTHONPATH"
export PATH

To make it work?

让它发挥作用?

采纳答案by Matthew Flaschen

Modifications to sys.pathonly apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATHenvironment variable:

修改sys.path仅适用于该 Python 解释器的生命周期。如果要永久执行,则需要修改PYTHONPATH环境变量:

PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH

Note that PATHis the system path for executables, which is completely separate.

请注意PATH,这是可执行文件的系统路径,它是完全独立的。

**You can write the above in ~/.bash_profileand the source it using source ~/.bash_profile

**您可以将上述内容写入其中~/.bash_profile并使用它的来源source ~/.bash_profile

回答by Aidan Ewen

Not sure why Matthew's solution didn't work for me (could be that I'm using OSX10.8 or perhaps something to do with macports). But I added the following to the end of the file at ~/.profile

不知道为什么 Matthew 的解决方案对我不起作用(可能是我使用的是 OSX10.8 或者可能与 macports 有关)。但我在文件末尾添加了以下内容~/.profile

export PYTHONPATH=/path/to/dir:$PYTHONPATH

my directory is now on the pythonpath -

我的目录现在在 pythonpath 上 -

my-macbook:~ aidan$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/path/to/dir', ...  

and I can import modules from that directory.

我可以从该目录导入模块。

回答by Tennom

Mathew's answer works for the terminal python shell, but it didn't work for IDLE shell in my case because many versions of python existed before I replaced them all with Python2.7.7. How I solved the problem with IDLE.

Mathew 的答案适用于终端 python shell,但在我的情况下它不适用于 IDLE shell,因为在我将它们全部替换为 Python2.7.7 之前存在许多版本的 python。我是如何用 IDLE 解决这个问题的。

  1. In terminal, cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
  2. then sudo nano idlemain.py, enter password if required.
  3. after os.chdir(os.path.expanduser('~/Documents'))this line, I added sys.path.append("/Users/admin/Downloads....")NOTE: replace contents of the quotes with the directory where python module to be added
  4. to save the change, ctrl+x and enter Now open idle and try to import the python module, no error for me!!!
  1. 在终端, cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
  2. 然后sudo nano idlemain.py,如果需要,输入密码。
  3. os.chdir(os.path.expanduser('~/Documents'))这一行之后,我添加了sys.path.append("/Users/admin/Downloads....")注意:用要添加 python 模块的目录替换引号的内容
  4. 保存更改,ctrl+x 并输入 现在打开空闲并尝试导入 python 模块,对我来说没有错误!!!

回答by Andros

Setting the $PYTHONPATH environment variable does not seem to affect the Spyder IDE's iPython terminals on a Mac. However, Spyder's application menu contains a "PYTHONPATH manager." Adding my path here solved my problem. The "PYTHONPATH manager" is also persistent across application restarts.

设置 $PYTHONPATH 环境变量似乎不会影响 Mac 上的 Spyder IDE 的 iPython 终端。但是,Spyder 的应用程序菜单包含一个“PYTHONPATH 管理器”。在此处添加我的路径解决了我的问题。“PYTHONPATH 管理器”在应用程序重新启动时也是持久的。

This is specific to a Mac, because setting the PYTHONPATH environment variable on my Windows PC gives the expected behavior (modules are found) without using the PYTHONPATH manager in Spyder.

这是特定于 Mac 的,因为在我的 Windows PC 上设置 PYTHONPATH 环境变量会提供预期的行为(找到模块),而无需在 Spyder 中使用 PYTHONPATH 管理器。