我应该把我的python文件放在venv文件夹的哪里? 您所在的位置:网站首页 pythonwhl文件应该放在哪里 我应该把我的python文件放在venv文件夹的哪里?

我应该把我的python文件放在venv文件夹的哪里?

2023-11-02 11:20| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

(Probably a noob question, but I didn't find a solution after googling for 20 minutes.)

I created a new pure Python project with PyCharm which yielded the following folder structure

myproject └── venv ├── bin │   ├── activate │   ├── activate.csh │   ├── activate.fish │   ├── easy_install │   ├── easy_install-3.5 │   ├── pip │   ├── pip3 │   ├── pip3.5 │   ├── python │   ├── python3 │   └── python3.5 ├── include ├── lib │   └── python3.5 ├── lib64 -> lib └── pyvenv.cfg

Where do I put myproject.py or the myproject folder now?

Inside or outside of venv? In the venv/binfolder? Just inside venv, i.e. myproject/venv/myproject.py? 推荐答案

The virtual environment manages files which aren't yours. It doesn't care how you manage your own files. Put them wherever makes sense to you, just not anywhere inside the venv directory tree. Common solutions include directly in myproject, or in myproject/src.

For what it's worth, one of the important use cases for virtual environments is the ability to delete one and start over. You obviously can't do that if you put stuff there which isn't part of the virtual environment. Regard them as ephemeral infrastructure.

Another use case is the ability to have multiple virtual environments for the same project, so that you can test that your code works with different versions of the libraries you depend on, or even different Python versions.

A common convention is to collect the libraries you need in requirements.txt so that you can create a new virtual environment, activate it, and pip install -f requirements.txt whenever you need to.

其他推荐答案

I guess you misunderstood the term "Virtual Environment". It provides an isolated environment wherein you can download a different version of python packages and run it for your project. Hence, do not put anything inside your virtual environment. Keep it clean.

To take advantage of the virtual environment,

activate it (source path_to_virtual_env/bin/activate ) install the necessary python packages using pip (pip install XYZ) and run your python code using python command (python3 mycode.py)


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有