简体   繁体   中英

How can I use editors/IDEs with a virtual environment | python and venv?

Now, I have learned that using virtual environments for your project is nice, and I learned how to use built-in venv module with my python3.7 installation.

Suppose I have created a virtual environment with this path

~/Desktop/Project1/venv

Now, I know how to activate, deactive the virtual environment etc. However, what I could not figure out is, the next step.

I can create .py files in project folder and compile them using python script.py of course, but using bash all the time is I suppose not the best way.

Assume I want to use sublime text to write and run my scripts, I could not figure out how I can make it possible.

I know how to change preferred python interpreter by adding a new build profile to sublime text.

It was pretty understandable when I was dealing with global python3.7 environment.

But I am confused how to implement this into this new virtual environment.

So, please provide me a solution to this.

Thanks.

If you can make your tools use the python binary placed within the directory of your virtual environment, then you should be all set (no need to activate and desactivate the virtual environment). The full path should be something like:

  • /path/to/venv/bin/python
  • C:\\path\\to\\venv\\Scripts\\python.exe

If everything is setup right the other Python executable scripts (such as pip , pylint , pep8 , and so on) are also located in that same directory and can be used directly without activating the virtual environment. But as always I'd recommend using the executable modules instead of the scripts whenever possible ( /path/to/venv/bin/python -m pip somecommand instead of /path/to/venv/bin/pip somecommand ).

References :

You will need to access your .venv folder. Once you are there, find the path to "Scripts".

C:\Users\path>cd C:\Users\path\to\environment\.venv\Scripts
C:\Users\path\to\environment\.venv\Scripts>activate
(VENV) C:\Users\path\to\environment\.venv\Scripts>

From there, you find the venv environment that you created and apply it. Depending on the IDE, you need to learn how to select your environment. I am posting a link that might be helpful to you. This video shows how to use a virtual environment with VS Code.

How to install and use a virtual environment in Visual Studio Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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