shell中运行python文件

Python中想在shell中调用一个test.py文件里面的方法。

test.py文件里面的内容如下:

[python]  view plain  copy
 print ?
  1. def ListFea():  
  2.     print "this is myself"  
  3.   
  4. def ListFeaT(fea):  
  5.     print "this is:", fea  
  6.   
  7. def ListFeaR(fee, fea):  
  8.     print "this is:", fee, "add", fea  
  9.   
  10. class simpleTest(object):  
  11.     "simple test"  
  12.     def runone():  
  13.         print "runone"  
  14.     def runtwo(self):  
  15.         print "this has:",self  
  16.     def runthree(self,value):  
  17.         print "this is:",value  

 

如何在shell中调用py中的内容呢。

其实主要加几行就够了:

 

[python]  view plain  copy
 print ?
  1. import sys #引入sys库体  
  2. sys.path.append("C://myPython"#往系统路径中加入自己存放py文件的地址  

 

然后就可以开始通过import的方法导入相关的方法和内容了

[python]  view plain  copy
 print ?
  1. from test import * #从test.py文件中加载所有的内容。  

 

顺带提一下python文件进行编译的方法:

[python]  view plain  copy
 print ?
  1. import py_compile #导入编译库  
  2. py_compile.compile("C://myPython//test.py"#对test.py文件进行编译  

 

因为也是刚开始学习,所以可能一些名词用的不是很准确,大家包涵了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值