-1

I was wondering on how I would access another python code within my existing code? Lets say the code I want to bring over is: code1.py and the code I am working with is code2.py my idea on how to do this is:

import code1

At the very start of my code. Am I correct or is there another way of doing this? Thanks!

gdm
  • 59
  • 1
  • 6
  • 1
    What happened when you tried? – juanpa.arrivillaga Apr 19 '21 at 16:57
  • 3
    Does this answer your question? [split python source code into multiple files?](https://stackoverflow.com/questions/12412595/split-python-source-code-into-multiple-files) – Woodford Apr 19 '21 at 16:58
  • Sure, but make sure code1 is in a function or the file will run by it's self. – Buddy Bob Apr 19 '21 at 16:59
  • Hello gdm, before submitting a question, please be sure to read StackOverflow's [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). It will help other people offer a solution. In the case of this question it would have been nice to supply your current `code1.py` and `code2.py` files, what you tried to do and why it didn't work. Cheers mate! – Amélie Krejčí Apr 19 '21 at 18:56

1 Answers1

1

You can acces those functions with: code1.function(arg), where code1 is your code1.py and function is a function (def function(argument):) from this file.

More on that here

Oliver K
  • 33
  • 5