Skip to main content
 首页 » 编程设计

python之让 macports django 和 python 一起工作

2025年05月04日34tuyile006

我是一名网络开发人员,我有一个 Django 项目需要处理。

我在 macbook pro 上运行 mac OSX 10.6.5。我使用 macports 安装了 django 和 python 2.6。

我现在遇到了一些问题,可能与我的 PATH/PYTHONPATH 有关,它阻止我运行 django。

在终端 echo $PATH 中给出:

echo $PATH 
/Library/Python/2.6/site-packages:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin 

并且 echo $PYTHONPATH 什么也没给出。

安装的 sudo 端口给出:

sudo port installed 
The following ports are currently installed: 
autoconf @2.65_1 (active) 
automake @1.11.1_0 (active) 
bash-completion @1.1_8 
bash-completion @1.2_0 (active) 
bzip2 @1.0.5_3+darwin 
bzip2 @1.0.6_0 (active) 
db46 @4.6.21_6 (active) 
expat @2.0.1_0 
expat @2.0.1_1 (active) 
fontconfig @2.8.0_0+macosx (active) 
freetype @2.3.12_0+macosx (active) 
gdbm @1.8.3_2 (active) 
gettext @0.18_0 
gettext @0.18.1.1_2 (active) 
gperf @3.0.4_0 (active) 
help2man @1.38.2_0 (active) 
ImageMagick @6.6.2-0_0+q16 (active) 
jpeg @8a_0 (active) 
lcms @1.19_2 (active) 
libiconv @1.13.1_0 (active) 
libpng @1.2.43_0 (active) 
libtool @2.2.6b_1+darwin (active) 
libxml2 @2.7.7_0 (active) 
m4 @1.4.14_0 (active) 
ncurses @5.7_0+darwin_10 
ncurses @5.7_1 (active) 
ncursesw @5.7_0+darwin_10 
ncursesw @5.7_1 (active) 
openssl @1.0.0b_0 (active) 
p5-locale-gettext @1.05_2 (active) 
p7zip @9.04_0 (active) 
perl5 @5.8.9_0 (active) 
perl5.8 @5.8.9_3 (active) 
pkgconfig @0.25_0 (active) 
py26-distribute @0.6.14_0 (active) 
py26-django @1.2.3_0+bash_completion (active) 
python26 @2.6.6_0+no_tkinter (active) 
readline @6.1.002_0 (active) 
sqlite3 @3.7.3_0 (active) 
tiff @3.9.2_3+macosx (active) 
xorg-bigreqsproto @1.1.0_0 (active) 
xorg-inputproto @2.0_0 (active) 
xorg-kbproto @1.0.4_0 (active) 
xorg-libice @1.0.6_0 (active) 
xorg-libsm @1.1.1_0 (active) 
xorg-libX11 @1.3.3_0 (active) 
xorg-libXau @1.0.5_0 (active) 
xorg-libXdmcp @1.0.3_0 (active) 
xorg-libXext @1.1.1_0 (active) 
xorg-libXt @1.0.8_0 (active) 
xorg-util-macros @1.7.0_0 (active) 
xorg-xcmiscproto @1.2.0_0 (active) 
xorg-xextproto @7.1.1_0 (active) 
xorg-xf86bigfontproto @1.2.0_0 (active) 
xorg-xproto @7.0.16_0 (active) 
xorg-xtrans @1.2.5_0 (active) 
zlib @1.2.5_0 (active) 

当我输入 python 时,我得到:

python 
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)  
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>>  

我很确定这是错误的版本。

当我尝试测试 django 是否可用于 python 时,我得到:

>>> import django 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ImportError: No module named django 
>>>  

谁能帮我弄清楚这里发生了什么?我只需要让 django 运行起来,这样我就可以查看和处理同事发给我的 django 项目。

提前感谢您的帮助!

请您参考如下方法:

首先,Macports 写入文件 ~/.profile 来设置它的 PATH 变量。如果您创建了 ~/.bash_profile 文件,那么 ~/.profile 将被忽略。您必须将内容复制过来。

要查看 Macports 选择的 python 版本,请使用:

port select --list python 

它将向您显示如下内容:

Available versions for python: 
    none 
    python25-apple 
    python26-apple 
    python27 (active) 
    python32 

要告诉 Macports 使用特定版本,请使用:

port select --set python python27 

这应该让你的 python 版本正确。您现在可以使用 python easy_install 安装 Django 或使用 Django 的 Macports 发行版。