I have a python file (my_code.py) in Home/Python_Codes folder in ubuntu. I want to run it in python shell. How can I do that?
I do this
>>> execfile('~/Python_Codes/my_code.py')
but it gives me path error
I have a python file (my_code.py) in Home/Python_Codes folder in ubuntu. I want to run it in python shell. How can I do that?
I do this
>>> execfile('~/Python_Codes/my_code.py')
but it gives me path error
Importing your module will execute any code at the top indent level - which includes creating any functions and classes you have defined there.
james@Brindle:/tmp$ cat my_codes.py
def myfunc(arg1, arg2):
print "arg1: %s, arg2: %s" % (arg1, arg2)
print "hello"
james@Brindle:/tmp$ python
Python 2.7.5 (default, Jun 14 2013, 22:12:26)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_codes
hello
>>> my_codes.myfunc("one", "two")
arg1: one, arg2: two
>>>
To add ~/Python_Codes to the list of places that python will search, you can manipulate sys.path to add that directory to the start of the list.
>>> import sys
>>> print sys.path
['', ... '/Library/Python/2.7/site-packages']
>>> sys.path.insert(0,'/home/me/Python_codes/')
>>> import my_codes
Go to Run >> cmd >> change directory to your Python folder , remember to put your file my_file.py into that folder. For example: if your Python folder in C drive, type
cd C:\Python
then type this
python my_file.py
the system will run your file.
Home/Python_Codesdoesn't make sense..pyfile in terminal usingpython ~/Python_Codes/my_code.py