简体   繁体   English

Python导入模块错误,“导入错误:无法导入名称X”

[英]Python Importing Module Error, “Import Error: cannot import name X”

I have two python modules, one named testingDirectories.py and another named testFile.py 我有两个python模块,一个命名为testingDirectories.py,另一个命名为testFile.py

There are two folders in my directory: simulations and src 我的目录中有两个文件夹:Simulations和src

"simulations" is supposed to hold all the source code for individual simulations that are being run. “模拟”应该包含正在运行的各个模拟的所有源代码。

"src" holds the source code for the main project that I run the simulations on. “ src”包含用于运行模拟的主项目的源代码。

My testingDirectories.py file is in "simulations", while testFile.py is in "src". 我的testingDirectories.py文件位于“模拟”中,而testFile.py文件位于“ src”中。

The contents of testFile.py are as follows: testFile.py的内容如下:

def testFunction(m):
    return 'hello, world'

The contents of testingDirectories.py are as follows: testingDirectories.py的内容如下:

import sys
import os

from src.testFile import testFunction

testFunction('hello')

When I run the simple demo, it gives me an error: 当我运行简单的演示时,它给我一个错误:

from src.testFile import testFunction
ImportError: cannot import name testFunction

The point of this demonstration is to figure out if the program can find the directory "src" without telling it to look in the root directory. 本演示的重点是弄清楚程序是否可以找到目录“ src”而不告诉它在根目录中查找。 It seems to be able to find other folders and their modules and functions, but not a simple test function like this one. 它似乎能够找到其他文件夹及其模块和功能,但是找不到像这样的简单测试功能。 Any ideas why I might be getting this error? 有任何想法为什么我可能会收到此错误?

You'll need a master script, placed in the same directory as simulations and src . 您将需要一个主脚本,该脚本与simulationssrc放在同一目录中。 From there, you can execute all the other scripts. 从那里,您可以执行所有其他脚本。 Or just include src to simulations , and add some __init__ files. 或者只是将src包括到simulations ,并添加一些__init__文件。

Alternatively, you can make this structure: 另外,您可以建立以下结构:

|mypackage
    |__init__.py
    |simulations
        |__init__.py
        |testingDirectories.py
    |src
        |__init__.py
        |testFile.py

The __init__.py is fine being empty. __init__.py可以为空。 Step two, include it in the PYTHONPATH . 第二步,将其包含在PYTHONPATH

$ set PYTHONPATH=%PYTHONPATH%;C:\my_python\mypackage

and then, in testingDirectories.py : 然后,在testingDirectories.py

from mypackage.src.testFile import testFunction
testFunction('hello')

Another way? 其他方式? Execute the program with -m switch. 使用-m开关执行程序。

$ cd C:\my_python\mypackage\simulations\
$ python -m simulations.testingDirectories

And you can import the second module with just from src.testFile ... 您可以from src.testFile ...导入第二个模块from src.testFile ...

Hope this helps! 希望这可以帮助!

What I ended up doing (because apparently relative paths are a fairly common problem in Python): 我最终做了什么(因为显然相对路径是Python中一个相当普遍的问题):

I created a file named main.py that does a 我创建了一个名为main.py的文件,该文件

os.walk('.')

to discover all files and folders and stores the module names in a dictionary for easy viewing. 查找所有文件和文件夹,并将模块名称存储在字典中以便于查看。

I then gave main.py a few functions to allow the user to simply run my scripts from the commandline through main.py, rather than having to find each individual module and run it from there. 然后,我为main.py提供了一些功能,使用户可以通过main.py从命令行简单地运行我的脚本,而不必查找每个单独的模块并从那里运行它。

This is the code I used to run modules and functions using a user-defined string: 这是我使用用户定义的字符串来运行模块和功能的代码:

def runModuleFunction(moduleName, funcName, params):
    m = __import__(moduleName)
    func = getattr(m, funcName)
    return func(params)

This solution works perfectly, so far. 到目前为止,此解决方案效果理想。

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

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