0

I was originally having issues installing packages in Python 3.5, but I found that the command line must be used instead of the IDLE shell to install Python.

Now that I have successfully installed the xlwt package, I am unable to import it in IDLE Python shell to try it out.

How do these pieces related to each other?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
skurp
  • 389
  • 3
  • 13
  • 1
    Can you provide an example of what you're doing in the shell, and what the response is? – Nathaniel Ford Oct 28 '15 at 22:35
  • You have probably installed `xlwt` using `pip` for a Python 2.x interpreter (see `which python`) - use `pip3` to install it for `which python3` instead, or use a `virtualenv` to explicitly specify a combination of interpreter and installed packages. However, that's just a guess (and assumes a bash-type terminal). The *"answer that can thoroughly explain how this works, including where/how stuff is stored in relation to Python/programming"* would be rather too long for SO. – jonrsharpe Oct 28 '15 at 22:36

1 Answers1

0

Depending on your environment, you may already have Python 2 installed (this is true of Mac OSX, for instance). Having installed Python 3, to access it you need to do something like this:

(pyenv)rook: nateford$ python3
Python 3.4.3 (default, Mar 10 2015, 14:53:35) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlwt
>>> 

No ImportError occurs, so we know it worked. To determine what python you're using you can do this:

(pyenv)rook: nateford$ which python
/Users/nateford/virtual_environments/pyenv/bin/python
(pyenv)rook: nateford$ which python3
/Users/nateford/virtual_environments/pyenv/bin/python3
(pyenv)rook: nateford$ python --version
Python 2.7.1
(pyenv)rook: nateford$ python3 --version
Python 3.4.3

(Note your results will vary because I'm using a virtual environment set up on my box.)

This question addresses IDLE for different versions of Python: you should be able to access it based on your version:

(pyenv)rook: nateford$ which idle
/opt/local/bin/idle
(pyenv)rook: nateford$ which idle3
(pyenv)rook: nateford$ which idle3.4
/opt/local/bin/idle3.4
(pyenv)rook: nateford$ which idle3.5
(pyenv)rook: nateford$ 

(Again, note I'm on the 3.4 version of Python.)

Similarly, when you install packages on the command line, if you are installing them for Python version 3, you are better off using pip3 than pip. (Technically, there is some subtly here, but for a command line newcomer it's easier to think of it that way.)

Community
  • 1
  • 1
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • So the command "python3" in terminal sets the environment to use the 3.5 version of python instead of the default Mac OS X Python 2? Now When I use the easy_install xlwt command in the terminal window, I receive an 'invalid syntax' error. – skurp Oct 28 '15 at 22:55
  • No... you should think of `python3` as a name that points to a particular file, which is executed. In this case that file (indirectly) points to the python command line interpreter for version 3. It doesn't do anything globally. Note the `>>>` indicates that you're at the prompt for the command line python interpreter. (Also known as a REPL.) – Nathaniel Ford Oct 28 '15 at 22:58
  • I see. Now in the command line python interpreter, how does this differ from the Python shell (IDLE)? What is the correct syntax for simple installation of packages at this point? – skurp Oct 28 '15 at 23:03
  • You will always have to install packages on the command line, so that they're available to the system. Then, depending on the shell you're using (IDLE versus the command line REPL), you will have packages available to you depending on how you installed them (`pip` or `pip3`) and what you're using (`idle` vs `idle3.5`). Does that make sense? – Nathaniel Ford Oct 28 '15 at 23:04
  • Yes thank you, that offers much more explanation than just solving this small issue! My problem stemmed from my misunderstanding about how different versions of each module and package are run. All it took was using the command "pip3 install xlwt" instead of just pip. Thanks a bunch! – skurp Oct 28 '15 at 23:13
  • @WilliamConnell Glad to help! Be sure to upvote/accept answers on StackOverflow that help you: people are much more willing to help people who participate! – Nathaniel Ford Oct 28 '15 at 23:20