Mac*_*hon 54 python macos add pythonpath
我想
import sys
sys.path.append("/home/me/mydir")
Run Code Online (Sandbox Code Playgroud)
将一个dir附加到我的pythonpath
如果我打印sys.path我的目录在那里.
然后我打开一个新命令,它就不存在了.
但不知何故,Python无法导入我保存在该目录中的模块.
我究竟做错了什么?
我读过.profile或.bash_profile就可以了.
我必须添加:
PATH="/Me//Documents/mydir:$PYTHONPATH"
export PATH
Run Code Online (Sandbox Code Playgroud)
为了使它工作?
Mat*_*hen 71
修改sys.path
仅适用于Python解释器的生命周期.如果要永久执行此操作,则需要修改PYTHONPATH
环境变量:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
请注意PATH
,这是可执行文件的系统路径,它是完全独立的.
**您可以使用上面的内容~/.bash_profile
和来源编写source ~/.bash_profile
shu*_*lik 26
which python
在 MAC OS 上,您可以使用命令或轻松找到 python/python3 的位置which python3
。(也适用于 Linux)
它应该给出类似的内容:
对于蟒蛇
/usr/local/bin/python
Run Code Online (Sandbox Code Playgroud)
对于Python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Run Code Online (Sandbox Code Playgroud)
将路径导出到您的bash_profile
在你的终端类型中
sudo nano ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)
输入您的密码并粘贴以下行
PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.9/bin/python3"
export PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
按control + x
退出,并y
在要求保存时按保存
按“enter”返回终端窗口
在终端中使用以下命令获取它,运行
source ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)
python3 的路径现在应该更新!!
不知道为什么马修的解决方案对我不起作用(可能是我使用OSX10.8或者可能与macports有关).但是我在文件的末尾添加了以下内容~/.profile
export PYTHONPATH=/path/to/dir:$PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
我的目录现在在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', ...
Run Code Online (Sandbox Code Playgroud)
我可以从该目录导入模块.
归档时间: |
|
查看次数: |
197731 次 |
最近记录: |