python安装os模块_Python:ImportError:没有名为os的模块

1586010002-jmsa.png

error_log

...

[Fri Sep 07 16:30:14 2012] [error] import os

[Fri Sep 07 16:30:14 2012] [error] ImportError: No module named os

-shell-

[root@lts5srv1 home]# ldd /root/epd-5.1.0/bin/python

libpython2.5.so.1.0 => /root/epd-5.1.0/lib/libpython2.5.so.1.0 (0x00002b0829205000)

libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da0600000)

libdl.so.2 => /lib64/libdl.so.2 (0x0000003d9fe00000)

libutil.so.1 => /lib64/libutil.so.1 (0x0000003dadc00000)

libm.so.6 => /lib64/libm.so.6 (0x0000003da0200000)

libc.so.6 => /lib64/libc.so.6 (0x0000003d9fa00000)

/lib64/ld-linux-x86-64.so.2 (0x0000003d9f600000)

Anybody know how to solve this error? it's the only error that i get when i try to run a python script or a django.wsgi script in a redhat system, after that i'm going to upload my web-app in a university server... and i've already configuret the VirtualHost. So that was the only problem, can anybody help please? Thank You.

解决方案

I found a solution for that problem (in redhat system), this is a solution in relation with Django framework..

I've installed Python 2.6 (so i don't use anymore the default installed python), and reinstalled Django using the new version of Python and some other modules that were required like: (easy_isntall, MySQL_python-1.2.3-py2.6-linux-x86_64, setuptools, mod_wsgi-2.5) so after install all these modules should compare in the site-packages directory of Python 2.6: "/usr/local/lib/python2.6/site-packages".

[root@lts5srv1 Python-2.6.8]# ./configure --enable-shared --prefix=/usr/local

[root@lts5srv1 Python-2.6.8]# make

[root@lts5srv1 Python-2.6.8]# make install

Configure mod_wsgi to link with Python 2.6 shared libs

[root@lts5srv1 /]# cd /usr/local/lib/python2.6/config/

[root@lts5srv1 config]# ln -s ../../libpython2.6.so .

[root@lts5srv1 mod_wsgi-2.5]# ./configure --with-python=/usr/local/bin/python2.6

[root@lts5srv1 mod_wsgi-2.5]# make

[root@lts5srv1 mod_wsgi-2.5]# make install

Then setting the environment variables:

[root@lts5srv1 Python-2.6.8]# export LD_LIBRARY_PATH=/usr/local/lib/python2.6

[root@lts5srv1 Python-2.6.8]# export LD_RUN_PATH=/usr/lib64/httpd/modules

Editing ~/.bashrc and adding some lines to keep the changes permanent:

[root@lts5srv1 ~]# vi ~/.bashrc

# .bashrc

# User specific aliases and functions

PATH=/root/epd-5.1.0/lib/python2.5:/root/epd-5.1.0/bin:${PATH}

PYTHONPATH=/usr/local/lib/python2.6

LD_LIBRARY_PATH=/usr/local/lib/python2.6

LD_RUN_PATH=/usr/lib64/httpd/modules

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

Django Install:

[root@lts5srv1 Django-1.4.1]# /usr/local/bin/python2.6 setup.py install

Then, checking if the modules are correctly integrated:

[root@lts5srv1 /]# ldd /usr/lib64/httpd/modules/mod_wsgi.so

[root@lts5srv1 /]# ldd /usr/local/bin/python2.6

这个错误提示 "ImportError: attempted relative import with no known parent package" 意味着你在尝试导入模块 `convert_to_valid_einsum_chars` 时使用了相对导入(`from ..parser import`),但在当前的代码上下文中,Python无法识别出这是从哪个父包开始的。这通常发生在以下几个情况: 1. 你可能在模块级别的文件中使用了相对导入,但模块所在的目录结构中没有定义一个明确的父包(如`parser`模块不是一个单独的包,而是直接在项目的根目录下)。 2. 你可能忘记在包含该导入语句的文件或模块上方添加适当的`__init__.py`文件,这样Python才能识别这是一个包。 解决这个问题的步骤有: - **确认包结构**:确保导入的模块在一个有效的包结构中,比如创建一个名为`parser`的文件夹,并在其中有一个`__init__.py`文件。 - **修正导入**:如果是模块级别的导入,考虑改为绝对导入(`import parser.convert_to_valid_einsum_chars`),或者确保在当前文件的上一级目录中引入父包。 - **调整路径**:如果你确实想保持相对导入,确保你正在正确的目录层级运行你的脚本,或者使用`os.path.dirname(__file__)`获取正确的工作目录。 如果你能提供更多的代码背景信息,我可以给出更具体的帮助。相关问题: 1. 你的项目是否按照标准的Python包结构组织? 2. 这段错误代码是在尝试执行什么特定操作时出现的? 3. 有没有其他的导入尝试也报类似错误?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值