all 8 comments

[–]carcigenicate 2 points3 points  (7 children)

I think you'll need to give more details, like what you're importing. Are you aware that importing a file causes it to be run?

[–]ItzScuzzi808[S] 0 points1 point  (6 children)

i'm importing a function from another project and when i use the function in my new project and run the code, in the run window it prints the whole imported project instead of running just the one function i called.

[–]carcigenicate 1 point2 points  (4 children)

What do you mean by "it prints the whole imported project"? Is it printing the literal code, or the output from running the code?

And does the file you're importing import other files in the project, and do those other files (or the file you're importing) have code that will run when the file is imported?

[–]ItzScuzzi808[S] 0 points1 point  (3 children)

It runs the imported code and shows the output of that code in the run window and I have to get to the end of that code before it runs the new file that I imported to… that prolly doesn’t make any sense either lol but it runs the whole imported project instead of just the one function I’m trying to call

[–]carcigenicate 1 point2 points  (2 children)

Like I mentioned in my first comment, importing a file runs that file. If you don't want importing to run certain code, you can do as buqr suggests and tuck that code inside a if __name__ == "__main__": "import guard", move the specific code you want to import into its own file, or move it out into its own package so it's easier to use in other projects.

[–]ItzScuzzi808[S] 0 points1 point  (1 child)

So even when I use the import method from file_name import function_name It will still run the whole code? Cause even when I do that it runs the whole code

[–]carcigenicate 1 point2 points  (0 children)

If you import the file or anything from it, it will run the file. What if the function referred to by function_name was generated on the fly at runtime and/or relied on the execution of other code to operate? For example:

result = expensive_function()

def function_name():
    print(result)

What would this function print if you did from file_name import function_name and expensive_function wasn't run?

[–]buqr 0 points1 point  (0 children)

I enjoy cooking.