如何打开多个.py程序

时间:2017-03-23 21:05:11

标签: python eclipse multiple-files

我有一个.py文件的程序(python 3.6)(我认为这叫做脚本),我曾经在python基本IDE 3.6中编码,然后我在shell中尝试了它,最后我用它制作双clic在.py文件中。
现在,我遇到了Eclipse(使用PyDev),现在我在这个IDE中编写代码。我了解到我可以将我的程序拆分为几个.py文件以使程序更有条理,然后我发现我可以使用带有倍数.py文件的多个包来使所有内容更有条理。 其实我的节目是:

Game
    /.settings                    # I didn't make it folder...
               /org.eclipse.core.resources.prefs          # ????
    /core                         # "main" package.
         /__pycache__               # I didn't make this folder but it have a lot of files that i used to use (put their are .pyc not .py). Can i delete them???
         /__init__.py               # I don't use it for now, but i read that all __init__.py files are very important... right?
         /main.py                   # This was my first file, for my this is the core, this file imports all the others, when i run the program in eclipse, i run this file. When the program was only a file, it was the file.
         /name_Data.csv             
         /registration.py           
    /functions                    # package with only functions (def)
              /__pycache__         
              /__init__.py
              /functions.py        
    /variables                    # package with only variables (config...)
              /__pycache__         
              /__init__.py
              /config.py           
              /variables.py        
    /.project                     # I didn't make it file...
    /.pydevproject                # I didn't make it file....

文件夹Game也存储在:

Workspace
         /Python
                /Game
                /.metadata        # ???
                          /.mylyn
                                 /.taskListIndex
                                                /segments.gen
                                                /segments_1
                                 /contexts
                                 /.task.xml.zip
                                 /repositories.xml.zip
                                 /task.xml.zip
                          /.plugins
                                    ... a lot of folders org.eclipse.jdt. etc...
                          /.back_0.log
                          /.lock
                          /.log
                          /version.init

现在,当我测试我的代码时,我总是从eclipse运行它(我不再使用python IDE),但是今天下午我试图打开程序,在main.py中制作doble clic并且它没有'工作,它也没有在python IDE中工作。当我制作doble clic时,它打开一个python控制台(抱歉我的低知识,我认为这称为Shell)一秒钟和关闭(没有打印任何东西),当我在python IDE中打开它打印:

Traceback (most recent call last):
File "C:\Users\####\Desktop\Extra\Workspace\Python\Game\core\main.py", line 25, in <module>
from variables import variables as var
ModuleNotFoundError: No module named 'variables'

如何在没有Eclipse的情况下打开它? 另外,我应该将main.py文件放在包外还是在核心包中?

2 个答案:

答案 0 :(得分:0)

从variables.py导入#the variables

应该这样做。 (我认为)

答案 1 :(得分:0)

你可以使用

from file1 import *

实施例

var.py

x = "Hello"

main.py

from var import *

print(x)

当你运行main.py时,它将打印Hello

编辑:在更仔细地阅读您的错误消息后,似乎是因为文件存在于不同的文件夹中

为了解决昨天刚刚做的项目

我有以下工作区

测试/

------&GT; test.py

contactMetrics.py

要导入contactMetrics.py,我必须执行以下操作

import os                                                                                    
import sys                                                                                   
path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.pardir, os.pardir))
sys.path.append(path)

from ContactMetrics import ContactMetrics