简体   繁体   中英

Python imports don't work on windows

Im trying to import a variable from another file. The problem is I get a ImportError: cannot import name 'issues' on line 6 in variables.py
The strange thing is, this works just fine on my ubuntu laptop but not on my windows main computer. I've googled a bit and apparently it has something to do with relative paths, so I've tried adding a . in front of parser, but then it gives the error SystemError: Parent module '' not loaded, cannot perform relative import .
Any idea whats causing this?

parser.py:

#!/usr/bin/env python3

## Settings
fileIn = 'trending_today.in'
fileOut = 'trending_today.out'

print("Parse file...", end=" ")

## Parse file
with open(fileIn) as f:
    content = f.readlines()
content = [x.strip() for x in content]

## Parse lines
issues = []
for i in range(len(content)):
    issues.append(content[i].split(' '))

for i in range(len(issues)):
    for j in range(len(issues[i])):
        issues[i][j] = int(issues[i][j])

print("done.")

## Functions for writing
def writeFile(commands):    # Input [[]]
    print("Write output file...", end=" ")
    for i in range(len(commands)):
        writeLine(commands[i])
    print("done.")
    return

def writeLine(line):    # Input: []
    string = ' '.join(line) # Convert array to line
    f = open(fileOut, 'a')
    f.write(string + '\n')
    f.close()
    return

variables.py (which I execute):

#!/usr/bin/env python3

## Import parser
from .parser import issues

print("Setup variables...", end=" ")

## Settings
number_videos = issues[0][0]
number_endpoints = issues[0][1]
number_request_decs = issues[0][2]
number_caches = issues[0][3]
cache_capacity = issues[0][4]

videos = issues[1]  # Size of videos

## Endpoints
endpoints = []  # 0 = datacenter ms
                # 1 = [ [cache_nr, cache_ms] ]
c = 2
for i in range(number_endpoints):
    ep = issues[c]
    endpoints.append([])
    endpoints[i].append(ep[0])
    endpoints[i].append([])
    for j in range(ep[1]):
        cache = issues[c+j+1]
        endpoints[i][1].append(cache)
    c += ep[1]
    c += 1

requests = []   # 0 = Video
                # 1 = Endpoint
                # 2 = Requests
for i in range(number_request_decs):
    requests.append(issues[c+i])

print("done.")

Whrn you run you program, does it print " Parsing file... done. ", before reporting the error, or just " Parsing file... "?

If it prints the second message, I suspect that the issues is that the file " trending_today.in " is not in the current working directory, and the open() is failing with an exception, which means the import gets aborted before issues gets set.

You will either need to change your working directory to the appropriate location, or ensure that the file is there.

======

Since neither of the outputs are appearing, the script is picking up the parser module in the standard library, rather than the local one.

Either rename " parser.py " to something that is not part of the library, or set PYTHONPATH to either " . " or the name of the directory. I suspect on the unix version, PYTHONPATH is already set.

Adding a " __init__.py " file may also work.

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