如何从python中的同一目录导入文件?

时间:2019-03-13 10:11:40

标签: python import

我在python中具有以下目录结构。

├── mel2samp.py
├── tacotron2
│   ├── layers.py

在mel2samp.py中,我想使用这些代码行从tacatron2.layers导入TacotronSTFT

import sys
sys.path.insert(0, 'tacotron2')
from tacotron2.layers import TacotronSTFT

但是它抛出一个错误 ImportError: No module named tacotron2.layers

3 个答案:

答案 0 :(得分:1)

还需要import java.util.Arrays; /** * The class Grid does the initialization of the game of life and the application of the rules. * It is a 2D array of the type Cell. It saves the cells and uses a copy of it to apply the rules. * */ public class Grid { public int col; public int row; public int x,y; public Cell [][] array; //2D array of the type Cell public Cell [][] arraycopy; /** * This constructor is to create the initial generation of cells and set the state of random 20% to true (=alive). * @param col the number of columns * @param row the number of rows * */ public Grid(int col, int row) { this.col = col; this.row = row; this.array = new Cell [col][row]; //Loops through every spot in the 2D array for (int x = 0; x < col; x++) { for (int y=0; y < row; y++) { //set randomly 20% of the cells' state to "true" if (Math.random() <= 0.2) { Cell cell = new Cell (true); this.array[x][y]= cell; } else { Cell cell = new Cell (false); this.array[x][y]= cell; } } } } /** * This method will count the alive cells in a 3*3 neighboorhood and apply the rules of life. * This method uses arraycopy. * */ public void lifeSteps() { //Works with a copy of the array and the cells this.arraycopy = new Cell [col][row]; for (int x = 0; x < col; x++) { for (int y=0; y < row; y++) { this.arraycopy [x][y] = new Cell(this.array[x][y].getState()); } } //Looping through the cells, but the cells at the edge are skipped for (int x = 1; x < col-1; x++) { for (int y= 1; y < row-1; y++) { //Looping through all the neighbors int numNeighborsAlive = 0; for (int i = x-1; i <= x+1; i++) { for (int j = y-1; j <= y+1; j++) { //In a 3x3 neighborhood the middle cell needs to be skipped if ((x != i) && (y != j)) { //Only the cells that are alive (true) are added if (arraycopy [i][j].getState() == true) { numNeighborsAlive += 1; } } } } //Apply the rules of life if ((array [x][y].getState()) && (numNeighborsAlive < 2 || numNeighborsAlive >3)) //Loneliness and Overpopulation { array[x][y].setState(false); } else if ((array [x][y].getState() == false) && (numNeighborsAlive ==3)) //Birth { array[x][y].setState(true); } else { //stasis } } } } /** * This method will return the statement for the array. * @return the 2D array of the type Cell */ public Cell[][] returnGrid () { return this.array; } /** * This method will test if everything is working well by printing zeros and ones. * */ public void printTest() { System.out.println("\t"); // a new line for (int x = 0; x < col; x++) { for (int y=0; y < row; y++) { // assigns 1 if the cell is alive and 0 if it is dead if (array[x][y].getState() == true) { System.out.print("1"); } else { System.out.print("0"); } } System.out.println(""); // will be displayed as colums and rows } System.out.println("\t"); // a new line } } 文件夹中的空__init__.py文件。之后,您可以执行以下操作:

tacotron2

答案 1 :(得分:1)

import sys
sys.path.insert(0, 'tacotron2')
from tacotron2.layers import TacotronSTFT
# Use TacotronSTFT

但是建议通过添加init.py来使tacotron2成为软件包

然后您可以将其用作

from tacotron2.layers import TacotronSTFT #Use TacotronSTFT

答案 2 :(得分:1)

您可以通过添加__init__.py
将文件夹打包 您可以详细了解here

  

需要__init__.py文件来使Python将目录视为包含包;这样做是为了防止具有公用名的目录(例如string)无意间隐藏了以后在模块搜索路径上(更深层)出现的有效模块。在最简单的情况下,__init__.py可以只是一个空文件,但它也可以为程序包执行初始化代码或设置__all__变量,如后所述。