How do I run a function from another script in Python

Teroro

Lets say FileA and FileB are my two files that do stuff. FileA has as an import FileB has some variables and functions. The Function I want to call in FileA from FileB is X which is using some other variables and functions of FileB. When I try to run the program I get an error saying that "Cannot acces local variable if it's not defined". Now I imagine that it would get all the stuff I declared in FileB but it doesn't. How can I go about this without making my program have a ton of references? Can't I just run my function from FileA while using FileB contents? Note that I am a beginner and it might be a stupid question! And also I will compile the files togheter at the end if it's of any help.

EDIT: If I declare Pas inside my function it does work! But I have too many things to define inside it. Why won't it pick up the context from FileB ?

Example in Code: FileA

import FileB as fb

print(fb.X())

FileB

import random
global Pas
p = "ok"
u = [some randomly generated stuff]

def RandomLetterGen:
   some code

Pas = ""
def X():
 Pas += RandomLetterGen()
 return Pas

This is an easy to understand example but my code does much more to get to the result of the X function in FileB

I tried looking it up online but no luck.

Austin

Do you need to initialize Pas inside the X() function?

def X():
 Pas = ""
 Pas += RandomLetterGen()
 return Pas

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I run another script in Python without waiting for it to finish?

How do I create a .spec file from a python script and then run another python script to execute pyinstaller with that spec file

How do I access a function from another script (unity)

How to run some code in python script, then pause script and wait for function call from another script

How do I run a Python script from C#?

How do I run a Python script from C#?

How do I run a PowerShell script with parameters from Python

Python - How do I give a value from one script to another?

How do I run a script in PyCharm that incorporates another script from the same project? (both scripts are in PyCharm)

How do I call globals() from another imported function in Python?

How do I call a member function from another file in python

How do I call a list from another function in Python?

How do I call a python function from a cgi script with javascript?

How to run & stop python script from another python script?

How do I run another file in python?

How do I install and run a python script

How do I pass the result from a command to another command's option in a single npm run script?

How do I run a function in the background of another function by using threads in python?

How do I call a function inside of another function from java script file in html file?

SwiftUI - How do I run code from the Coordinator of another Struct from a function inside a VStack?

How do I run commands in a script as another user?

How do i run this "run_scaled" script from github

How do I call an array from another function in Javascript/ Google App script

How do I properly run a separate python script from main python file?

How do I run a command using the output of another function in powershell?

How do I run PnP Script on Azure Function App

How do I tell my PHP script to run a Javascript function?

How do I run python script with command line arguments from MS Visual Studio 2015

How do I activate python anaconda and run script from regular command prompt in Windows 10