IDLE in a Virtual Environment

Using tools like IDLE in a virtualenv isn't as straightforward as it should be. I'll show you how to launch it so your custom project packages are available and how to use a requirements file to quickly build up an environment.

You can always get a list of installed packages with pip freeze. If you're in a virtualenv, you'll get the virtualenv's list of packages. If you're not, you'll get a list of your global packages. pip freeze > requirements.txt is the fast way to generate your requirements file the first time.

Requirements files always have the same layout: package_name==1.2.3. You can also use <, >, <= and >=, too (e.g. Django>=1.9,<2.0 would select the latest version of Django that's newer than, or equal to, 1.9 and less than 2.0).

0:01

So I want to show something special here.

0:03

If I open up Idle, let's just go
find it again, like we did before.

0:09

All apps, Idle, there we go.

0:10

Okay so I installed flask but here if
I do import flask, I can't get to it,

0:17

because I installed it in a virtualenv and
I didn't install it globally.

0:21

So if I want to use Flask to write and
run my software that is

0:27

inside a virtualenv,
I have to launch idle in a special way.

0:31

So we do python-m,

0:33

because again we're gonna use
a special module, and we do idlelib.

0:39

And, so now we've got this.

0:40

And If I do import flask,
then no errors because it finds flask.

0:46

All right.

0:47

So, that's important to know.

0:49

Inside of a virtualenv is different
from outside the virtualenv.

0:53

So what if our virtualenv gets all
screwed up and we need to start over?

0:58

I said in the last video that these
were meant to be disposable, so

1:01

let's dispose of this one.

1:03

Before we do that though, let's get
a list of the things we have installed so

1:06

that we can install those again.

1:08

We do that using the pip
command of freeze.

1:12

So pip freeze tells us we have
these five packages installed.

1:16

And you'll see that there's
the package name on the left

1:19

flask it's dangerous jinja two etcetera.

1:21

And then the stable equal means
install exactly this version and

1:25

then on the right we have the version to
exactly install so, we want flask version

1:30

zero point ten point one not flask 0.10.0
or 0.10.2 or 0.5.2 or anything like that.

1:34

We want exactly 0.10.1.

1:35

Now we could copy and paste that.

1:37

I don't like either.

1:39

I'm kinda lazy.

1:40

I don't wanna copy and
paste that into a file.

1:47

So let's do something
computer do at force.

1:51

Computers are good at doing things, right?

1:53

All right.
So we do pit freeze again.

1:56

But before we press enter,
we're gonna do a greater than symbol, and

2:00

then we'll give it a file name.

2:03

And we're gonna say requirements
which I can't type.

2:07

Requirements.txt and
we're gonna press return.

2:12

And if we do a dir we see that there's
a requirements file in here right now.

2:17

And, if we cat it, then it prints out the
same thing as we did print prix before.

2:24

So, the requirements .txt file
name isn't a requirement [LAUGH],

2:28

but, it is a common practice and
some third party services like Heroku, for

2:33

example, expect that file name.

2:35

Alright so let's trash,

2:38

let's clear our screen, let's trash this
virtual environment and make a new one.

2:43

So, I'm still in it, I'm still activated,
so let's get out of it.

2:47

Let's deactivate and let's get rid of it.

2:52

So, the way we get rid of
a directory is we do RM, and yes,

2:56

I want to delete that, alright,
and now I'm starting over.

3:03

So I need a new virtualenv,
so let's do phyton dash

3:08

mvenv and
let's call this one flask basics two,

3:13

this is my second try and
it builds the virtualenv for

3:17

me and we have our requirements.txt file.

3:22

And then we're gonna do pip install -r

3:26

.\requirements.txt and
it'll install everything.

3:31

Now, I did have a couple of
issues with line endings.

3:34

If you have issues with line endings
to where it's like what is it?

3:39

Embedded null character.

3:41

It could be that your line
endings are something that

3:43

pip isn't currently understanding.

3:45

Just make sure the file
is saved just like UTF-8.

3:48

And the line endings are Windows or

3:52

Linux should both work, but
swap it back and forth if we need to.

3:55

Your text editor should be able to do
that unless your text editor is idle.