简体   繁体   English

在 Visual Studio 之外启动 python virtualenv 代码

[英]Launching python virtualenv code outside of Visual Studio

I have a Python code in Visual Studio which reads input, opens a file based on it, then makes a pyplot based on the file and saves the plot.我在 Visual Studio 中有一个 Python 代码,它读取输入,基于它打开一个文件,然后根据该文件制作一个 pyplot 并保存绘图。 All files are in the same folder as the .sln file.所有文件都与 .sln 文件位于同一文件夹中。

To use the matplotlib package, I have used virtual environment.为了使用 matplotlib 包,我使用了虚拟环境。 That led to creating "env" folder, but I can not find my application there.这导致创建“env”文件夹,但我在那里找不到我的应用程序。

So I would like to know a way how can I launch the code via doubleclicking a .py application, instead of launching Visual Studio and pressing Ctrl+F5 every time.所以我想知道如何通过双击 .py 应用程序启动代码,而不是每次启动 Visual Studio 并按 Ctrl+F5。 Simply copying the code into "mycode.py" obviously doesn't work.简单地将代码复制到“mycode.py”中显然是行不通的。

You need to activate the virtualenv in order to execute your Python code inside your virtual environment.您需要激活 virtualenv 才能在您的虚拟环境中执行您的 Python 代码。

To activate the virtual environment you have to type the following in the terminal:要激活虚拟环境,您必须在终端中键入以下内容:

On macOS and Linux:在 macOS 和 Linux 上:

source env/bin/activate

On Windows:在 Windows 上:

.\env\Scripts\activate

You can confirm you're in the virtual environment by checking the location of your Python interpreter, it should point to the env directory.您可以通过检查 Python 解释器的位置来确认您处于虚拟环境中,它应该指向 env 目录。

On macOS and Linux:在 macOS 和 Linux 上:

which python
.../env/bin/python

On Windows:在 Windows 上:

where python
.../env/bin/python.exe

To execute your python file just type python myfile.py in the terminal instead of double-clicking it.要执行您的 python 文件,只需在终端中键入python myfile.py而不是双击它。

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

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