-1

I would like to define a bunch of commands in a separate file for tidiness.

def cmd1():
    print("calling cmd1")

import define_cmd2

cmd2()

And then define_cmd2.py:

cmd1()

def cmd2():
    print("called cmd2")

It tells me cmd1() is not defined. How can I call import so that I can use/edit/call stuff defined before the import call?

BaleineBleue
  • 139
  • 2
  • 12
  • You might think your naming convention here is very smart, but it actually just confuses the reader. Consider re-writing this with good realistic names and proper formatting. – Alan Kavanagh Sep 25 '17 at 17:55
  • Please give proper examples for your two files. I can't actually understand which file should be importing what – roganjosh Sep 25 '17 at 17:56
  • is that better? – BaleineBleue Sep 25 '17 at 17:58
  • 1
    That's not how python works. Are you hoping for some sort of reverse-import? – Ng Oon-Ee Sep 25 '17 at 17:58
  • Where is the `import` statement in `define_commands`? – Mad Physicist Sep 25 '17 at 17:58
  • 2
    Possible duplicate of [Circular (or cyclic) imports in Python](https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – Mad Physicist Sep 25 '17 at 17:59
  • @MadPhysicist from context it's clear that he expects define_commands to USE the def which is in the file which is importing it.... – Ng Oon-Ee Sep 25 '17 at 17:59
  • 1
    @NgOon-Ee. That is not clear to me at all. Nothing is :) – Mad Physicist Sep 25 '17 at 18:00
  • I would like to import the file and just execute the code in the current scope. Is that possible? – BaleineBleue Sep 25 '17 at 18:00
  • 1
    Why should `cmd1()` be recognised in define_cmd2.py? It looks like your import is the wrong way round. You want to import `cmd1()` into define_cmd2.py – roganjosh Sep 25 '17 at 18:04
  • its an example name, it would actually do something different in my code. I have a bunch of variables and functions defined in the main file. then partway through I need to call the second file to define a bunch of functions, then continue with my code – BaleineBleue Sep 25 '17 at 18:06
  • 1
    Classic XY problem I think. You need to think the question through more thoroughly. The way you wish to do this is not possible (which does not mean you cannot achieve what you actually want to, just that the only answer TO THE QUESTION YOU ACTUALLY ASKED is no). – Ng Oon-Ee Sep 25 '17 at 18:08
  • @NgOon-Ee. That is why there is an "unclear what you asked" option when closing questions. Just sayin'. – Mad Physicist Sep 25 '17 at 18:13
  • @MadPhysicist fair enough – Ng Oon-Ee Sep 25 '17 at 18:16

2 Answers2

0

A python file (e.g. define_cmd2.py) is/should be self-contained. A very basic requirement is that if you wish to use a function named cmd1, it MUST be defined either in define_cmd2.py or in some file which is imported before you call the function.

However this sounds more like an XY problem. What are you actually trying to do?

Ng Oon-Ee
  • 1,193
  • 1
  • 10
  • 26
  • I want to execute `define_cmd2.py` in the middle of my code. I am defining a bunch of functions in `define_cmd2.py` and just for tidiness I would like to have them all in a different file. – BaleineBleue Sep 25 '17 at 18:10
  • Why does define_cmd2 meed cmd1 then? It sounds like define_cmd1 should be a separate file, perhaps imported twice (once in your 'main' and once in define_cmd2. @MadPhysicist answer would be relevant then. – Ng Oon-Ee Sep 25 '17 at 18:14
0

You should first understand how Python imports work a little.

The first time you invoke import on a module, it gets executed. The resulting namespace, with all function and variable definitions is used to create a module object that is referenced in your namespace, as well as in sys.modules. The next time it gets imported, the import just references the existing module from sys.modules.

To avoid errors caused by cyclic imports, the module object is actually created first, before the code is run, so that further imports will already see an existing module in sys.modules and not attempt to re-execute the same code over and over.

In your particular case, you need to explicitly import define_cmd2 from define_cmd1 if you want to use the contents of define_cmd1. Secondly, you need to reference the imported names properly:

define_cmd1:

def cmd1():
    print("calling cmd1")

import define_cmd2

define_cmd2.cmd2()

define_cmd2

import define_cmd1

import define_cmd1.cmd1()

def cmd2():
    print("called cmd2")

The bolded items are bits of code you need to add.

To clarify, here is what will happen when you run define_cmd:

  1. The def statement will create a function define_cmd1.cmd1.
  2. The import will attempt to load define_cmd2
  3. The import in define_cmd2 will do nothing since sys.modules['define_cmd1'] already exists, meaning that it is loaded or being loaded.
  4. define_cmd1.cmd1() will run.
  5. The def statement will create a function define_cmd2.cmd2
  6. Return to loading define_cmd1
  7. Run define_cmd2.cmd2
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Would execfile work for what I am after? I have heard it is unstable, why is that? – BaleineBleue Sep 25 '17 at 18:14
  • 1
    @ZachMozel. Noone here has any idea of what you are after. You really need to come up with a better example of what you are doing. – Mad Physicist Sep 25 '17 at 18:14
  • Have you tried what I suggest and had it fail, or are you just asking questions without any application in mind? – Mad Physicist Sep 25 '17 at 18:16
  • @zachmozel I'm personally struggling to understand why you would want to do this in the first place. If you're separating functions between files "to make things clearer" then when do both functions need to be available in both files? There's no clear execution path to me in your example. Your example now has 2 modules seemingly running the same code. – roganjosh Sep 25 '17 at 18:16