You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从其他目录的函数中导入变量?代码实现遇阻求助

How to Access a Local Variable from a Method in Another File

The core issue here is that variable is a local variable inside my_function—it only exists while that function is executing, and isn't exposed to the outside by default. Importing the Repo class won't give you direct access to it because it's not an attribute of the class/instance or a module-level variable.

Here are three practical solutions depending on your use case:


Solution 1: Turn variable into an instance attribute

If the variable should be tied to a specific instance of Repo, make it an instance attribute using self:

first.py

class Repo:
    def my_function(self):
        # Initialize as an instance attribute
        self.variable = []
        # Add any logic to populate the variable here

second.py

import sys
# Replace 'path/to/first_py_directory' with the actual folder containing first.py
sys.path.insert(1, 'path/to/first_py_directory')
from first import Repo

def my_second_function():
    # Create an instance of Repo
    repo = Repo()
    # Run my_function to initialize the variable
    repo.my_function()
    # Now you can access and modify the variable
    print(repo.variable)

# Test the function
my_second_function()

Solution 2: Return variable from my_function

If you don't need to keep the variable linked to the Repo instance, simply return it from the function:

first.py

class Repo:
    def my_function(self):
        variable = []
        # Populate the variable as needed
        return variable

second.py

import sys
sys.path.insert(1, 'path/to/first_py_directory')
from first import Repo

def my_second_function():
    repo = Repo()
    # Capture the returned variable directly
    variable = repo.my_function()
    print(variable)

my_second_function()

Solution 3: Use a class-level variable (for shared state)

If variable should be shared across all instances of Repo, define it as a class variable:

first.py

class Repo:
    # Class-level variable (shared by all Repo instances)
    variable = []
    
    def my_function(self):
        # Modify the class variable (use Repo.variable to ensure class-level access)
        Repo.variable.append("updated value")

second.py

import sys
sys.path.insert(1, 'path/to/first_py_directory')
from first import Repo

def my_second_function():
    # Access directly via the class (no instance needed)
    print(Repo.variable)
    # Or modify it via an instance
    repo = Repo()
    repo.my_function()
    print(Repo.variable)

my_second_function()

Quick Note on Paths

Double-check that the path you add to sys.path is the parent directory of where first.py resides. For example, if first.py is in ~/code/my_project/core, insert ~/code/my_project/core into sys.path.

内容的提问来源于stack exchange,提问作者user12533975

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:37:38