1

Similar questions have been asked for other languages (C++, Clojure, TypeScript, maybe others) but I am still looking for an answer for Python.

There are a lot of similar questions related to the use of import and global in Python but the associated answers don't fit my needs. I just want to split a big file into smaller ones to easily modify/reuse parts of the code in different versions of the same program, without having to deal with different namespaces or managing global variables.

A simple hack to do what I want would be to merge selected Python files at runtime with a script but I hope there is a pythonic way of doing that.

With an illustration, what I am trying to do is going from several big files which are almost identical:

big_file_v1.py

## First part
# Hundreds of code lines to define things, make computations...

## Second part
# More code to do a few additional things

big_file_v2.py

## First part
# Exactly the same first part as in big_file_v1.py

## Second part
# A lot of small differences compared to big_file_v1.py

...to several smaller files with most of them not needing any modification or only needing modifications that I want to share across all of the different versions:

myprog_v1.py

include first_part_common_to_both_versions.py

include second_part_v1.py

myprog_v2.py

include first_part_common_to_both_versions.py

include second_part_v2.py

In this second case, using include-like commands, I can instantly share the modifications made in first_part_common_to_both_versions.py across version 1 and 2 of my program and I just need to modify/copy the smaller files second_part_v2.py if I want to make new modifications/create another new version.

The question is: how to do this include in Python?

Just to avoid debates on good software development practices, I use Python as a tool to solve scientific questions and as such, I care more about editing comfort than coding practice.

Falken
  • 929
  • 6
  • 9
  • I wish I had an explanation for the downvote. Why is such an acceptable question in other languages so bad in Python? – Falken Dec 21 '17 at 16:26
  • as much as we would like to have good questions, I second your doubt here. whoever downvotes it should explain what's wrong with it. In the meanwhile, according to [MCVE](https://stackoverflow.com/help/mcve) given you are new, if you could kindly demonstrate your problem with a concrete example, that'll be more helpful for us to figure it out and for you get a quick answer. I think this might be why the person downvoted you. I voted it back for you, in hope that you will edit your question. thanks a lot. – stucash Dec 21 '17 at 16:45
  • Do `from other_file import *` in the main file for each file you separated out? – Graipher Dec 21 '17 at 16:56
  • @stucash My question is not limited to the script I'm currently working on, that's why I didn't include code in it. Anyway, I tried to give an illustrative example. Thanks to you for stopping by, commenting and upvoting. – Falken Dec 21 '17 at 17:57
  • @Graipher But what if I have several files to include, all of them potentially reading *and* modifying the same (global) variables? I read somewhere that doing several `from some_file import *` would reinitialize the variables. – Falken Dec 21 '17 at 18:00
  • @Graipher My previous comment/answer was unclear: I mean, what if the "subfiles" have common dependencies? I would have to do a `from common_file import *` in all subfiles. I might even have crossed dependencies. I wish I could do kind of a _raw_ import, much like a command line `cat` but in Python. – Falken Dec 21 '17 at 18:14

1 Answers1

1

Have a look at exec or execfile:

"Python's exec statement is similar to the import statement, with an important difference: The exec statement executes a file in the current namespace. The exec statement doesn't create a new namespace. We'll look at this in the section called “The exec Statement”

John Richardson
  • 676
  • 8
  • 24
  • Thanks for the very good suggestion, but considering [this question](https://stackoverflow.com/q/436198/9094516), the new execfile in Python 3 sounds a bit inconvenient, especially because it makes debugging harder, missing the line numbers of the executed file (see one of the comments in the previous link). I will give it a try anyway and accept your answer if exec() is usable enough in practice... – Falken Feb 12 '18 at 17:40