简体   繁体   English

在虚拟环境中加载空闲时设置PYTHONPATH

[英]PYTHONPATH set when loading Idle in virtual environment

如何设置事物,以便将PYTHONPATH设置为特定项目,并且在加载空闲后所有虚拟环境模块都可用?

You can append it to environment like so: 您可以将其附加到环境中,如下所示:

import sys
if "N:/ProjectX/Scripts" not in sys.path:
    sys.path.append("N:/ProjectX/Scripts")

Edit: you can install PyYaml package to your python with pip , then make Default_Path.yaml somewhere in your main application directory and insert your project and paths like so: 编辑:您可以使用pipPyYaml软件包安装到python中,然后在主应用程序目录中的某个位置创建Default_Path.yaml ,然后插入项目和路径,如下所示:

 #Paths
 ProjectX:
     Icon: 'N:/ProjectX/ICON'
     Presets: 'N:/ProjectX/Presets'
 ProjectY:
     Icon: 'N:/ProjectY/ICON'
     Presets: 'N:/ProjectY/Presets'

and to read Default_Path.yaml you can run this: 并读取Default_Path.yaml可以运行以下命令:

import yaml
documents = open(N:/Application/Default_Path.yaml, 'r')
doc = yaml.load(documents) #It's a Dictionary
print doc['ProjectX']['Icon']

then you can check with users system and if the path doesn't exists append them to their environment. 那么您可以与用户系统进行检查,如果该路径不存在,请将其附加到他们的环境中。

Edit: reading sub-python files in a python project is simply by organize and add __init__.py in every sub-folders like so: 编辑:只需通过组织并在每个子文件夹中添加__init__.py即可读取python项目中的子python文件,如下所示:

D:/ #Driver name
    projectX/ #Project Name
        main.py #Main Application
        Mudules/ #Sub-folder
            __init__.py #initialize the sub-folder
            module1.py #package/moudle
            module2.py #package/moudle
        Widgets/ #Sub-folder
            __init__.py #initialize the sub-folder
            widget1.py #custom widget 
            widget2.py #custom widget 

visual example: 视觉示例: 在此处输入图片说明

I give my main file main.py because of c++, but in python you can give any name, it's personal preference. 由于c ++,我给了我的主文件main.py ,但是在python中,您可以给任何名字,这是个人喜好。

and to call your sub-python project in your main.py , you just write like so: 并在main.py调用您的sub-python项目,您可以这样编写:

import Mudules.module1 as moudle1
import Widgets.widget1 as coolUI

or 要么

from Mudules.module1 import *
from Widgets.widget1 import *

Then you just call you function. 然后,您只需调用函数即可。 and you don't need to check if the path is in sys.path and if not sys.path.append() for python files. 并且您不需要检查路径是否在sys.path ,如果不是,则检查python文件的sys.path.append()

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

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