Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split python source code into multiple files?

Tags:

python

I have a code that I wish to split apart into multiple files. In matlab one can simply call a .m file, and as long as it is not defined as anything in particular it will just run as if it were part of the called code. Example (edited):
test.m (matlab)

function [] = test()     ... some code using variables ...     test2 

test2.m (matlab)

... some more code using same variables ... 

Calling test runs the code in test as well as the code in test2.

Is there a similar way for python, to put ... some more code ..., into an external file, that will simply be read as if it is in the file that it is called from?

like image 888
Markus Avatar asked Sep 13 '12 18:09

Markus


People also ask

How do you split data into multiple files in Python?

The split() method will return a list of the elements in a string. By default, Python uses whitespace to split the string, but you can provide a delimiter and specify what character(s) to use instead. For example, a comma(,) is often used to separate string data. This is the case with Comma Separated Value (CSV) files.

How do I split a list in Python?

A list can be split using Python list slicing. To do so, we first read the file using the readlines () method. Next, the top half of the file is written to a new file called romeo_A.txt. We’ll use list slicing within this for loop to write the first half of the original file to a new file.

How to split a text file into multiple smaller files in Python?

In the following example, we’ll use list slicing to split a text file into multiple smaller files. A list can be split using Python list slicing. To do so, we first read the file using the readlines () method.

How to run two files at the same time in Python?

You can do the same in python by simply importing the second file, code at the top level will run when imported. I'd suggest this is messy at best, and not a good programming practice.

How to split a file into a list using a generator?

We can read the file and split the lines into a list with the splitlines () method. Afterwards, a for loop can be used to print the contents of the text data. In Python, a generator is a special routine that can be used to create an array. A generator is similar to a function that returns an array, but it does so one element at a time.


1 Answers

Sure!

#file  -- test.py -- myvar = 42 def test_func():     print("Hello!") 

Now, this file ("test.py") is in python terminology a "module". We can import it (as long as it can be found in our PYTHONPATH) Note that the current directory is always in PYTHONPATH, so if use_test is being run from the same directory where test.py lives, you're all set:

#file -- use_test.py -- import test test.test_func()  #prints "Hello!" print (test.myvar)  #prints 42  from test import test_func #Only import the function directly into current namespace test_func() #prints "Hello" print (myvar)     #Exception (NameError)  from test import * test_func() #prints "Hello" print(myvar)      #prints 42 

There's a lot more you can do than just that through the use of special __init__.py files which allow you to treat multiple files as a single module), but this answers your question and I suppose we'll leave the rest for another time.

like image 94
mgilson Avatar answered Sep 19 '22 18:09

mgilson