Understanding Virtual Environments and Pipenv

I’ve completed the Pipenv lesson in the Learn Python 3 course but I’ve been left with a lot of questions that Googling doesn’t seem to help answer, maybe because the questions cover a lot of disparate topics. I’m working in Windows 10 and using Visual Studio Code as my IDE.

I understand the purpose of virtual environments as a way to contain projects that work with different modules, but I don’t really understand how they work or how I work inside of them. So say I’m following the instructions in the video and have set up the virtual environment and downloaded my various modules. Now:

I access the shell in the virtual environment using
pipenv shell
and then open my python interpreter with
python3

That’s all well and good, but now that I’m here and coding… doesn’t my code get lost if I exit out of the terminal? How do I access or create a file so that I can save my code in there?

For the other question, let’s back up a bit. Say I’ve created the virtual environment but haven’t accessed the shell. In VS Code, I go to the folder with the Pipfile for the virtual environment. In that folder, I make a new .py file using the “New File” button:
new file creation

Now I can write, edit, and save my code in here via the text editor, but am I working in the virtual environment? And if I’m not, is it possible for me to work in the virtual environment while using the text editor?

Sorry if these questions are actually very basic, I’m just having a lot of trouble finding resources that help me understand this. Thanks in advance.

Do you know the command line? If not, there are several brief (& some not so brief) videos on YouTube. The CL is just navigating around your computer’s file system but you’re not using a UI. (For Mac, you’d use Terminal and for Windows you’re using the Command Prompt.) you can create directories, delete them, create files, move files, etc. etc. from the command line.

There’s also the docs for Pipenv here:
https://pipenv-fork.readthedocs.io/en/latest/

So…you would use the commands to make a directory for your project and then you’d initiate the pipenv for it.
Ex:

  • mkdir project && cd project to make a directory for the project and to “cd” change directories to that dir.

  • mkdir environment && cd environment --which makes a directory for the environment & the files

  • then you select whatever version of Python you’d be using in that env: pipenv --python 3.9 (or whatever version you’re using)

  • then you start the virtual environment: pipenv shell

  • then you can install whatever package names you’ll be needing for your project:
    pipenv install . Let’s say you need the requests library: pipenv install requests
    etc.

There’s a lot of pipenv resources out there, especially on YT. I like the CodingEntrpreneurs videos a lot b/c I think they explain stuff pretty thoroughly.
This is a good one too: https://youtu.be/zDYL22QNiWk and this one as well: https://youtu.be/0CKuaf11H5Q
And, https://youtu.be/KxvKCSwlUv8

2 Likes

Also, I don’t use VS code, but isn’t there a terminal window in VS code? You can see the file paths in the left hand side panel, no?

There are over 50+ threads about Pipenv here in the forums, so, don’t worry, you’re not alone with any confusion. I would also recommend reading some of the other posts as many of the same users have answered similar questions & delved out some good info.

I really only know how to navigate and work with directories in the command line; I have never worked with files in it before. I’ll take a look into that. And yes, you can see both the terminal and directory in VS Code, so that would help me if I get lost. Thanks for the help!

1 Like