[英]How to open a multiples .py program
I had a program (python 3.6) of one .py file (I think that is called script) that I used to code in python basic IDE 3.6, then I tried it in the shell and finally I used it making double clic in the .py file. 我有一个程序(python 3.6),其中有一个.py文件(我认为这是脚本),我曾经在python基本IDE 3.6中进行编码,然后在外壳中进行了尝试,最后在外壳中使用它制作了double clic。 py文件。
Now, I met Eclipse (with PyDev) and now I code in this IDE. 现在,我遇到了Eclipse(使用PyDev),现在我在此IDE中进行编码。 I learned that I can split my program into a few .py files to have the program more organized, and then I discover that I can have multiples packages with multiples .py files to have everything more organized. 我了解到可以将程序拆分为几个.py文件以使程序更有条理,然后发现可以将多个包与.py多个文件组合在一起,以使一切都井井有条。 Actually my program is: 实际上我的程序是:
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....
Also the folder Game is store in: 文件夹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
Now when I test my code I always run it from eclipse (I don't use python IDE anymore), but this afternoon I tried to open the program making doble clic in main.py and it didn't work, also it didn't work in python IDE. 现在,当我测试我的代码时,我总是从eclipse运行它(我不再使用python IDE),但是今天下午,我试图打开main.py中的doble clic程序,但是它没有用,也没有。在python IDE中工作。 When I made doble clic, it open a python console (sorry for my low knowledge, I think this is called Shell) for a second and close (without print nothing), when I open it in the python IDE it print: 当我做一个可疑的工具时,它打开了一个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'
How can I open it without Eclipse? 没有Eclipse如何打开它? Also, shall I have the main.py file outside a package or in core package? 另外,我应该在包之外还是在核心包中保存main.py文件?
From variables.py import #the variables 从variables.py导入#变量
That should do it. 那应该做。 ( I think) ( 我认为)
You can just use 你可以用
from file1 import *
Example 例
var.py var.py
x = "Hello"
main.py main.py
from var import *
print(x)
When you run main.py it will print Hello 当您运行main.py时,它将打印Hello
EDIT: After reading your error message more closely it would appear that is because the files live in different folders 编辑:更仔细地阅读您的错误消息后,它会出现是因为文件位于不同的文件夹中
To get around this on a project I just did yesterday 为了解决这个问题,我昨天才做了
I had the following workspace 我有以下工作区
tests/ 测试/
------> test.py ------> test.py
contactMetrics.py contactMetrics.py
To import contactMetrics.py I had to do the following 要导入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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.