简体   繁体   English

Python导入在Windows上不起作用

[英]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 问题是我收到一个ImportError: cannot import name 'issues'在variables.py的第6行上ImportError: cannot import name 'issues'
The strange thing is, this works just fine on my ubuntu laptop but not on my windows main computer. 奇怪的是,这在我的ubuntu笔记本电脑上可以正常工作,但在Windows主计算机上却无法正常工作。 I've googled a bit and apparently it has something to do with relative paths, so I've tried adding a . 我已经用Google搜索了一下,显然它与相对路径有关,所以我尝试添加。 in front of parser, but then it gives the error SystemError: Parent module '' not loaded, cannot perform relative import . 在解析器的前面,但是会出现错误SystemError: Parent module '' not loaded, cannot perform relative import
Any idea whats causing this? 知道是什么原因造成的吗?

parser.py: 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): variables.py(我执行):

#!/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... "? 在运行程序时,它会在报告错误之前打印“正在Parsing file... done. ”,还是只是显示“正在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. 如果它显示第二条消息,我怀疑问题在于文件“ trending_today.in ”不在当前工作目录中,并且open()失败并带有异常,这意味着导入将在设置issues之前被中止。

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. 将“ parser.py ”重命名为不属于库的一部分,或者将PYTHONPATH设置为“ . ”或目录名称。 I suspect on the unix version, PYTHONPATH is already set. 我怀疑在Unix版本上,已经设置了PYTHONPATH。

Adding a " __init__.py " file may also work. 添加“ __init__.py ”文件也可能有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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