LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2017, 11:25 AM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Rep: Reputation: Disabled
run python script from python command line


Hi there,

This has never occurred to me before, but up till now, my main.py file was inside the package, so I was able to run python, and from in there, just import the file and then run it. So now that I am learning about threading, it happened that I needed to run main.py from outside the package, as python was the main thread which in turn meant I could get the Daemon process I have written to stop.

So now main.py is outside the package, I now need a python window open to help me debug and test, but I can't for the life of me work out how you run a python file from inside python itself as in all my 4 years of part time python learning have I actually run a python script whist python has already been started.

How is this done please?
 
Old 03-14-2017, 06:05 PM   #2
hoes
Member
 
Registered: Sep 2005
Distribution: debian, linux from scratch
Posts: 183

Rep: Reputation: 39
I cannot quite understand what you are after, but I can give a few suggestions.
Run a script using : python name_of_script.py
Write a function that starts when the script is made:
Code:
def start_function():
    pass # put code here

if __name__=='__main__':
    start_function()
In the above snippet you get the same behavior if you call the code as a script and if you load it as a package and run start_function() yourself.

Again, I hope this helps. If not, try to write what you are missing.
 
1 members found this post helpful.
Old 03-15-2017, 07:03 AM   #3
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Original Poster
Rep: Reputation: Disabled
ok, so linux: in terminal:
Code:
someone@linux: python3

python3.....
>>>
from here, how can I run a python script?
 
Old 03-15-2017, 07:20 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,018
Blog Entries: 5

Rep: Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872Reputation: 2872
If you run python3 from the same directory that your script resides (easiest way if you're pulling in other scripts from within the same directory):

>>> exec(open('./mypythonfile.py').read())
 
Old 03-15-2017, 02:42 PM   #5
hoes
Member
 
Registered: Sep 2005
Distribution: debian, linux from scratch
Posts: 183

Rep: Reputation: 39
Why not run
Code:
python name_of_script.py
You could do the exec, but if it is just a function you run you can also:
Code:
>>>import name_of_script
>>>function()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why don't python scripts run unless I run them with explicit invocations of python? RandomTroll Linux - Newbie 23 10-06-2016 02:29 PM
I would like to run a linux command from within a python script Leroy1990 Linux - Newbie 7 02-28-2012 04:30 PM
How to close open ports using a python script or a shell script in python ?? apanimesh061 Programming 3 11-20-2011 01:31 AM
How do I make python programs run without entering the command python? trist007 Programming 5 03-22-2009 09:21 PM
Multithreaded python script calls the COMMAND LINE jCash Linux - Newbie 1 12-08-2006 09:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration