python在windows下import其他模块的注意事项
来源:互联网 发布:稳定的排序算法 编辑:程序博客网 时间:2024/05/21 19:25
初学python,在同一目录中创建了两个py文件:a.py和b.py,然后在a.py中import b.py
from . import b......
在linux下执行没有问题,但是在windows下就提示 ImportError: cannot import name 'b'
在网上搜了半天也没有找到问题所在,后来还是在google上找到了答案。
原来是python会在sys.path下去找import的python文件,我输出了一下sys.path:
import sysprint(sys.path)
sys.path里并没有我的项目文件夹,所以import的时候就会出错,用以下代码即可解决:
import sysimport ossys.path[0]=os.path.dirname(os.path.realpath(__file__))from . import b然后再 from . import b 就ok了!
如果更完善些,可以判断一下当前的运行环境,如果是linux,就不用设置sys.path了
import platform def isWindowsSystem(): return 'Windows' in platform.system() def isLinuxSystem(): return 'Linux' in platform.system()if isWindowsSystem() : import sys import os sys.path[0]=os.path.dirname(os.path.realpath(__file__))from . import b
还有另外一种更简单的办法:
import syssys.path.append("./")import a
阅读全文
0 0
- python在windows下import其他模块的注意事项
- Python import 其他文件夹下的模块
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- 如何在windows下的Python开发工具IDLE里安装其他模块
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- C++使用boost python import 其他路径的模块
- windows的python开发环境下如何安装其他模块(httplib2)
- Windows 下在 Python 中安装 Swampy 模块的方法
- 在Windows下如何安装Python的lxml模块
- python模块在windows下的几种安装方法
- from ....import导入其他路径的模块
- import 导入其他路径的模块
- python在windows下安装模块错误
- Python----Matplotlib模块在Windows下安装
- python动态import某个文件夹下的模块
- python shell命令行中import多层目录下的模块
- MATLAB/SIMULINK测量端问题
- SVN版本控制
- CentOS6开启FTP及telnet服务教程
- NOPI\\Excel导入数据
- Linux下几种RTP协议实现的比较和JRTPLIB编程讲解
- python在windows下import其他模块的注意事项
- Recycleview下拉刷新,上拉加载的思路
- java web开发问题集锦
- 《COM技术内幕——微软组件对象模型》(Inside COM)一书部分源码 for VS 2010
- WebApp图片缩放
- MySQL触发器使用详解
- Android Fingerprint -- HAL层的初始化工作
- Minimum Size for Bigfile Tablespace in 12c (Doc ID 2076125.1)
- Qt5.5在ubuntu下解决 GL/gl.h: No such file or directory 与 cannot find -lGL