简体   繁体   中英

Python included global variable is running whole function (file) - why?

i need to include global variable from one file to another and its including the whole function... and i dont know why???

my source file create_repo.py:

def create_repository(repository_name):
    headers = {
        'Authorization': 'Basic' +personal_access_token,
        'Content-Type': 'application/json',
        'User-Agent': 'Chrome'
            }

    payload = json.dumps({
        "project": "xxxxx",
        "name": repository_name
                        })



    global repository_id
    response = requests.request("POST", base_url, headers=headers, data=payload, 
    auth=HTTPBasicAuth("yyyy", personal_access_token))
    print(response.text)
    repository_id = response.text[6:44]
    print("ID of the new repository:" ,repository_id)
    



if __name__== "__main__":
    create_repository(sys.argv[1])

and second file in the same directory:

import create_repo
       print(create_repo.repository_id)

and this show me error: AttributeError: module 'create_repo' has no attribute 'repository_id'

and if is not in "main" functions it will include the whole file create_repo.py and is trying to create the repository one more time...

i have tryed multiple settings like from create_repo import * or from create_repo import repository_id etc... nothing worked.

so i made a change (no definition inside of a function) as you told me and now i have this issue: :"1","innerException":null,"message":" (is trying to make the existing repository again in the policies file but it was already created in create_repo.py file)

create_repo.py

headers = {
'Authorization': 'Basic' +personal_access_token,
'Content-Type': 'application/json',
'User-Agent': 'Chrome'
      }

 payload = json.dumps({
 "project": "xxxxx",
  "name": repository_name
                 })

 response = requests.request("POST", base_url, headers=headers, 
data=payload, auth=HTTPBasicAuth("yyyy", personal_access_token))
print(response.text)
global repository_id
repository_id = response.text[6:44]
print("ID of the new repository:" ,repository_id)

and the policies file

from create_repo import *
print(repository_id)

Try with the following code.

In the file create_repo.py :

# add the necessary import here
import ...

repository_id = ""

def create_repository(repository_name):
    headers = {
        'Authorization': 'Basic' +personal_access_token,
        'Content-Type': 'application/json',
        'User-Agent': 'Chrome'
            }

    payload = json.dumps({
        "project": "xxxxx",
        "name": repository_name
                        })



    global repository_id
    response = requests.request("POST", base_url, headers=headers, data=payload, 
    auth=HTTPBasicAuth("yyyy", personal_access_token))
    print(response.text)
    repository_id = response.text[6:44]
    print("ID of the new repository:" ,repository_id)

In the file policies.py:

import create_repo

if __name__== "__main__":
    create_repo.create_repository(sys.argv[1])
    print(create_repo.repository_id)

I have moved the if __name__== "__main__": from create_repo.py to policies.py .

Obviously you have to execute policies.py and not create_repo.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM