Working in Python 2 and 3

Here’s what to keep straight in your mind as you do Python assignments:

Zed (LPTHW) is all Python 2. Those of you on Mac OS: In any directory on your computer, when you type python in Terminal, you will launch Python 2.7.x. No virtualenv for this.

Mitchell and all scraping exercises use Python 3. In Terminal, navigate into your scraping directory. There, activate your virtual environment:

source env/bin/activate

BeautifulSoup is already installed there. You can run any Python file there, and it will run under Python 3.6.x, not 2.7.x. To run a file named foobar.py, for example:

(env) myname scraping$ python foobar.py

Remember, you don’t start Python to run programs that are in files.

When you get an error, first ask yourself: Am I in the correct Python environment? 2 or 3?

Second, make sure you are not in Python when you are trying to run a file. When you see >>> you are in Python. To run a file, you need to be at the bash prompt: $

To quit using the virtualenv:

(env) myname scraping$ deactivate

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s