python os.environ无效
时间: 2023-12-30 09:41:58 浏览: 220
在给定的代码中,os.environ['CUDA_VISIBLE_DEVICES'] = '1'设置的CUDA可见设备无效的原因是在torch引入之前进行的设定。根据代码结构,startup.py文件中的os.environ['CUDA_VISIBLE_DEVICES'] = '1'应该放在从tools.train中导入main函数之前。这样,当main函数被调用时,CUDA可见设备就已经正确设置了。因此,你需要将os.environ['CUDA_VISIBLE_DEVICES'] = '1'放在from tools.train import main之前。这样,os.environ['CUDA_VISIBLE_DEVICES']的设置就会在main函数被调用时生效。
示例代码可以是这样的:
```python
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
from tools.train import main
def startup(gpu=0, config1='xxx', config2='xxx'):
args = parse_args(config1, config2)
main(args)
if __name__ == "__main__":
startup(gpu=1)
```
这样,os.environ['CUDA_VISIBLE_DEVICES'] = '1'就会在main函数执行时生效了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [os.environ[‘CUDA_VISIBLE_DEVICES‘]无效的解决方法](https://blog.csdn.net/asd123pwj/article/details/126442265)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *3* [python os.environ 读取和设置环境变量](https://blog.csdn.net/weixin_39650994/article/details/110971897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文