简体   繁体   English

PyCharm 与命令行的虚拟环境对比

[英]Virtual Environment from PyCharm vs. Command Line

I am fairly new to creating Python applications.我对创建 Python 应用程序还很陌生。 I have fooling around with some small tutorials and applications using PyCharm and have always created a new project using the Virtualenv environment, ending up with a "venv" folder under my project folder.我玩弄了一些使用 PyCharm 的小教程和应用程序,并且总是使用 Virtualenv 环境创建一个新项目,最后在我的项目文件夹下有一个“venv”文件夹。 I have not had any problems with this, but then again I have not done any large projects.我对此没有任何问题,但话又说回来,我没有做过任何大型项目。

However, I have been wanting to learn Flask want to try to create a new Flask project the proper way.但是,我一直想学习 Flask 想尝试以正确的方式创建一个新的 Flask 项目。 I see in many tutorials that people are creating (and activating) the virtual environment from the (Windows/Linux) Command Line instead even though they are using PyCharm and I was just wondering what the difference is?我在许多教程中看到人们正在从(Windows/Linux)命令行创建(和激活)虚拟环境,即使他们使用的是 PyCharm,我只是想知道有什么区别?

When I work on a project in PyCharm, created with Virtualenv, I do not activate the venv before working on it.当我在使用 Virtualenv 创建的 PyCharm 中的项目上工作时,我在处理之前不会激活 venv。 Is this wrong or is this something that is handled by PyCharm?这是错误的还是 PyCharm 处理的东西? What if the venv is created from a Command Line?如果 venv 是从命令行创建的呢? Is it still handled (activated) by PyCharm if working on the project there.如果在那里处理项目,它是否仍由 PyCharm 处理(激活)。 And what about the folder structure?那么文件夹结构呢? Is this affected by how the virtual environment is created?这是否受虚拟环境创建方式的影响? Is there somewhere I can find some "best practices" for the setup / folder structure when creating Flask project within a Virtual Environment?在虚拟环境中创建 Flask 项目时,我是否可以在某个地方找到一些设置/文件夹结构的“最佳实践”?

PyCharm activates the VirtualEnv for you if it is configured to use one and told where it is (more specifically, where the respective Python binary in the VirtualEnv is).如果 PyCharm 配置为使用 VirtualEnv 并告知它在哪里(更具体地说,VirtualEnv 中相应的 Python 二进制文件在哪里),它会为您激活 VirtualEnv。

There's no real difference between manually created VirtualEnvs and ones created by PyCharm.手动创建的 VirtualEnvs 和 PyCharm 创建的 VirtualEnvs 没有真正的区别。 (Apart from the framework you select to create one in case this is different from what PyCharm is configured with.) (除了你 select 创建一个框架,以防这与 PyCharm 的配置不同。)

If you want, you can just create one manually and then point PyCharm to it.如果需要,您可以手动创建一个,然后将 PyCharm 指向它。 Either during creation of the project or later using the Settings dialog (see Settings -> Project -> Project Interpreter).在创建项目期间或稍后使用“设置”对话框(请参阅设置 -> 项目 -> 项目解释器)。 It will then treat it no differently and also activate it for you when working inside the IDE.在 IDE 内工作时,它不会有任何不同的处理并为您激活它。

A virtual environment is pretty much just a folder which stores installed Python packages and isolates them from the rest of your system.虚拟环境几乎只是一个文件夹,其中存储已安装的 Python 包并将它们与系统的 rest 隔离开来。 This is so you can work on different projects which may all have competing requirements for external packages, without getting into conflicts.这样您就可以处理可能对外部包都有竞争要求的不同项目,而不会陷入冲突。 "Activating" a virtual environment just sets certain environment variables in your current shell so it'll use packages from this environment. “激活”虚拟环境只是在您当前的 shell 中设置某些环境变量,因此它将使用来自该环境的包。 "Activating" an environment never has any impact beyond your current shell.除了您当前的 shell 之外,“激活”环境永远不会产生任何影响。 So activating an environment on the command line won't do anything to PyCharm.所以在命令行上激活环境不会对 PyCharm 做任何事情。

PyCharm integrates a Python interpreter to give you lots of extra functionality. PyCharm 集成了 Python 解释器,为您提供许多额外的功能。 You tell PyCharm which interpreter you want to use for your project and it'll figure out what packages it has available, what version it is, and automatically set everything up properly for running your code from PyCharm etc. You can tell PyCharm to use your system's Python interpreter or an existing virtual environment or even use it to create a new environment.你告诉 PyCharm 你想为你的项目使用哪个解释器,它会找出它有哪些可用的包,它是什么版本,并自动正确设置一切以从 PyCharm 等运行你的代码。你可以告诉 Z4149CE0EE30A909A28F620系统的 Python 解释器或现有的虚拟环境甚至使用它来创建新环境。 You don't need to do anything special beyond just selecting the right interpreter/environment in the project settings.除了在项目设置中选择正确的解释器/环境之外,您不需要做任何特别的事情。

There's no reason to activate the environment from the command line if you're not going to use it from the command line.如果您不打算从命令行使用环境,则没有理由从命令行激活环境。 Of course, using Flask and running its server from the command line and keeping it running in the background may be useful.当然,使用 Flask 并从命令行运行其服务器并使其在后台运行可能会很有用。 Not sure if PyCharm would give you an easy or integrated option to have persistent processes run in the background.不确定 PyCharm 是否会给您一个简单或集成的选项,让您在后台运行持久进程。 You could still select the same virtual environment in PyCharm and use it to run your tests in it directly from PyCharm, use its debugger etc.您仍然可以在 PyCharm 中使用相同的虚拟环境 select 并使用它直接从 PyCharm 中运行您的测试,使用它的调试器等。

I prefer to keep the venv out of the project folder and store all venvs in ~/.virtualenvs/ or such.我更喜欢将venv保留在项目文件夹之外,并将所有 venv 存储在~/.virtualenvs/等中。 It declutters the project folder and prevents accidentally checking those files into the version control system.它整理项目文件夹并防止意外将这些文件签入版本控制系统。

I was just wondering what the difference is?我只是想知道有什么区别?

There's many tools for creating and using virtual environments and there's no difference between them, the only difference between them is their commands syntax (or the way it interact with users, eg for Pycharm you set some settings via GUI).有许多用于创建和使用虚拟环境的工具,它们之间没有区别,它们之间的唯一区别是它们的命令语法(或它与用户交互的方式,例如对于 Pycharm,您可以通过 GUI 设置一些设置)。

Is this wrong or is this something that is handled by PyCharm?这是错误的还是 PyCharm 处理的东西?

There's nothing wrong with it.它没有任何问题。 As long as you have a venv (or .venv ) directory in the root of your project and it is executable for any user, Pycharm will use it and it activates this virtual environment for you (without telling you).只要您在项目的根目录中有一个venv (或.venv )目录并且它对任何用户都是可执行的,Pycharm 就会使用它并为您激活这个虚拟环境(不告诉您)。 If Pycharm was not able to do that, (because of trouble in finding venv or activating/executing it.) then it will show you messages to fix its problems and it can't run your project till you fix them.如果 Pycharm 无法做到这一点,(因为找不到 venv 或激活/执行它的麻烦。)然后它会显示消息来解决它的问题,并且在你修复它们之前它无法运行你的项目。

It's better to create your virtual environment in .venv directory right into the root directory of your project.最好在项目的根目录中的.venv目录中创建虚拟环境。 (It's kind of conventional) (有点传统)

See python virtual environments and configuring pycharm virtualenv as well.请参阅python 虚拟环境配置 pycharm virtualenv

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

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