ImportError when changing Python interpreter - python

I have been using PyCharm on Ubuntu to run some Python code, where in Edit Configurations I specified the interpreter path as /usr/bin/python2.7. The code uses the pygame module, and so to install this, I also ran sudo pip install pygame. Then I used import pygame in my Python script, and the file ran ok.
However, I now want to use Python 3.4. So, in PyCharm, I specified the interpreter path to be /usr/bin/python3.4. However, when I run the same file, I now get the error: ImportError: No module named 'pygame'.
Can somebody explain why this is happening? How can I get my Python 3.4 interpreter to find Pygame?
Thanks!

Each python installation has a separate set of libraries. Your python 3 does not know about Python 2 and its libraries. It seems the default pip command calls the python2 pip script. Run again the pip install, but with the python3 pip (look for it in your python3 folder, it is probably named pip3)

As others have pointed out, different Python installations will maintain their own independent set of libraries. Bear in mind that you can install packages from the same window that you use to change the interpreter in PyCharm for a project. You might find that more convenient if you're switching between interpreters.
You just click the green 'plus' icon to do so:

pygame has different releases for each version of Python.
As you have the Python 2.7 module and want to upgrade to Python 3.4, you'll need to reinstall pygame using Python 3.4 instead, you can using pip again or you can download files from here: http://www.pygame.org/download.shtml.

Related

Installing PyAutoGui on multiple versions of Python

I, as it will soon be obvious, am a total newb when it comes to Python.
I am running python version 3.5 on Windows 10, 64 bit.
I installed the PyAutoGui module for a small project I am working on. At first, everything worked perfectly. But now it appears that PyAutoGui is crashing when it clicks. I suspect that it's because PyAutoGui is only intended for use up to Python 3.4.
In order to rectify this, I downloaded Python 3.4. Unfortunately, however, when I try to install PyAutoGui (using pip install pyautogui), it tells me that it's already been installed because it sees it in the Python 3.5 folder.
My question is this: How do I install PyAutoGui in Python 3.4 with it already installed in Python 3.5?
Assume that I know virtually nothing about how to install a module manually without using pip
Thanks in advance!
If you have multiple versions of Python installed you need to find your versions and rename them and their Pips.
In windows the path is, C:\\Users\USERNAME\AppData\Local\Programs\Python\Python3x-32. The x should be replaced with the Python version and USERNAME with your username. On Mac it's located in /usr/local/bin/python. On Linux it should be in /usr/bin/python. The location might vary depending on OS and Python version.
Rename the files python.exe/python and pip.exe/pip so that each file is different. I named mine python35.exe, python2.exe and python.exe(for 3.5, 2.7 and 3.6).
Now when you execute your pip command use, pip34 install pyautogui or whatever you named the file.
Or if you really want to you can go the painful way of renaming all the path variables, but I won't explain that here.

Python module installed on Windows 10 with Ubuntu bash is not recognized

I have to use TensorFlow on my Windows 10 laptop, on which I have installed python 2.7, 3.2 and 3.4.2. I followed the instructions on the first answer here. I managed to install pip3 and tensorflow with the Ubuntu bash environment and I successfully tested TensorFlow on the command prompt. However, when I try to import it in my IEP environment (using Python 3.4), the module is not recognized. I am pretty new in managing python modules. What am I missing?
UPDATE: I found out that, when using Python via the ubuntu bash, the version is 3.4.3, but I never installed this version. Is there a way I can make it work with the versions I installed?
Looks like you are trying to make things complex to start with and having 3 (actually 4) python environment is making it even more harder if you are new to python. If you want to start using TF quickly and avoid platform specific problem, the quick route will be to remove most of platform 3.x installs and keep one if you really need 3.x otherwise you can do a lot with python 2.7. Once you have only 1 python environment, it will be very easy for you to manage and run faster. If you don't want to change anything and get you going with python do the following:
Install pip 8.1 or above
Install virtualenv for python 2.7 using pip - (This will help you to keep all the python specific modules within a specific folder)
Install TF for python 2.7 from https://www.tensorflow.org/get_started/os_setup
Use python 2.7 shell
If you run this command it will show which TF module you are using and where it is install (you must have TF installed):
python -c 'import os; import inspect; import tensorflow; print(os.path.dirname(inspect.getfile(tensorflow)))'

How do I change the kernel/python version for iPython?

I have installed iPython using pip in OS X 10.10, and it gave me the "ipython" and "ipython2" commands, which run great, but which use OS X's default python version 2.7.9. I downloaded and installed the latest release of Python3.4 and can load it with the command "python3," but cannot find a way to get iPython to use this version of python. The iPython Web site states the package can be used with python versions 3.3 and above, but I cannot find any instruction on how to change the default python version used.
So far I have found that the jupyter package for iPython has a kernel specification in /usr/local/share/jupyter/kernels/, which is just a folder called "python2" containing a json file that points to the system's python 2.7.6, but altering this to point to the new python3.4 installation does not work. My guess is this configuration is for the ipython notebook.
I've also tried the approach here: ipython reads wrong python version
In doing so I've duplicated the ipython2 command in /user/local/bin/ and edited it to use the python3 interpreter located at /Library/Frameworks/Python.framework/Versions/3.4/bin/python3, however, this gives me an error "ImportError: No module named 'IPython'," which suggests the python3 installation does not have ipython installed.
To tackle this, I've tried uninstalling ipython and reinstalling it using pip, but it just targets the system's Python 2.7 installation and does nothing for python3.
Does anyone know how to configure iPython to use a different python version, or even install a separate ipython installation for python3? Ultimately it would be nice to quickly switch back and forth depending on my needs.
I just found the answer. In essence, this stems from not understanding the python installation layout and how resources are separated between installed interpreters. It appears each python version will have its own repository of tools, and the current "pip" command I had installed on the system was mapped for use with python 2.7, so all libraries, tools, and other details it managed where available only to python 2.7. This included iPython.
I thought that installing python3 would make all these libraries available to the new interpreter by default, but it appears the system keeps them all separate (which makes sense). The main issue here was continuing to use "pip" which targeted the old installation, instead of the new "pip3" command included with python3. By using pip3 to reinstall iPython, I was able to get iPython3 installed properly.
Unfortunately this setup means needing to re-download other libraries, but that's not too difficult.
It might seem like a trivial issue in hindsight, but this had me completely stuck. I hope this helps someone else in this situation.

coding python - need to set default interpreter without screwing up system

I am a newbie Python programmer. I am coding a simple application right now that just runs commands and prints the output in an organized way. Specifically, commands from FGLRX, the proprietary driver for ATI/AMD GPUs cards for linux.
One of the functions, is to get the device temperature. So far I got it working, but id like to make the output refresh automatically. I was advised to use the curses library.
Setting it up, I realized Im probably not running the best python system config.
Here is what happens:
I am running on Debian Wheezy. I am using the PyCharm Python IDE with project interpreter Python 3.3
So, first of all, when I try to debug within the IDE, it fails. I googled that, and thats because curses can not find a terminal within the IDE. - that's fine.
So I was trying to debug by just running "naturally" (per say) the program from Konsole.
I am not even sure what Python version my system is running. But it does not seem to be 3 because it fails to operate properly my program when using "elif".
But my biggest issue is the default subversion of python3 running. Its not the one I want.
Why do I say that:
running a python console within my IDE (3.2.3), I check for installed modules and both "curses" and "_curses" shows up as installed.
But, in my Konsole, if I run "python3 main.py", I get this:
audric#pc1:/mnt/megaomg/software/AmdAtiUtils$ python3 main.py
Traceback (most recent call last):
File "main.py", line 13, in <module>
import curses
File "/usr/local/lib/python3.4/curses/__init__.py", line 13, in <module>
from _curses import *
ImportError: No module named '_curses'
So apparently I am running 3.4 as default Python3 and it does not have curses installed?
My question here is what is my best option?
Set my default Python3 to 3.2.3?
Re compile 3.4 with curses?
go emo and go back to windows?
yet another option?
My goal here is to be able to run my program (obviously) but without compromising my system stability.
This is the program by the way:
https://github.com/audricd/AmdAtiUtils
Thanks in advance.
First, to know what is the Python version installed in your system, run in a terminal:
$ python --version
Second, curses are available on Python 3.4 standard library. Well, actually, as Documentation says:
The Windows version of Python doesn’t include the curses module.
So, if you are running your code on a Windows machine, you should read the documentation linked above to get one alternative.
Right, now your question. Some usual way to have a Python interpreter isolated from your system is using virtualenv. You can grab it from pip easily:
# pip3 install virtualenv
After, you can create a virtual environment in this way:
$ virtualenv venv
And it will generate a fresh interpreter for you isolated from your system Python binds (note that it will create a directory called venv). To activate it:
$ source venv/bin/activate
And now you can install all the necessary modules for your project without messing with your system (and perhaps with other Python code that you have).
Also, virtualenv lets you to specify the Python version which it will be based on. To do it, you should already have this specifc Python version installed on your system, and you need know where is its correspondent binary. So,you can do:
virtualenv venv --python=/the/python/binary/here
After you have set up virtualenv and have activated it, you can simple type:
python your_app.py
Now, your code will run with the Python version that you have set up with virtualenv.
You can get more on virtualenv on its documentation.
If you deliberately want to use a Python specific version and you don't have it installed on your system, download it from Python website and follow these instructions found in the Python README which is included in your donwload. If you need help, there is already many questions on StackOverflow related on how to install a new Python version without messing with your system.

Installing Pillow for Python on Windows

I am fairly new to Python and trying to install the Pillow package on Windows 7. I downloaded and ran the MS Windows installer Pillow-2.2.1.win-amd64-py3.3.exe from here. It appeared to install fine. If I run the simple line of code:
from PIL import Image
directly from the Python interpreter, it runs fine. A help() on PIL gives me the package contents.
But when I try to run the same line of code from within a script, I get an ImportError: No module named PIL. What am I missing?
(Note that I've been able to import sys and import MySQLdb from within scripts just fine.)
Resolved: sure, enough, I'm running Python 2.7 when I run scripts. (I think I vaguely recall having to install an older version so I could interface with MySQL.) Thank you all for pointing out that I should check the version being used.
For third-party modules for Windows, my go-to resource is Christoph Gohlke's Python Extension Packages for Windows. You can find the latest version of Pillow here. Make sure you're working with the python.org version of Python.
As far as your specific error, it's hard to tell exactly without a traceback, but make sure your script is calling the correct version of Python. If you have also installed Python 2.7, for example, your script may be calling that instead of 3.3.
In such cases I'm simply printing the sys.path at the beginning of the script in trouble and comparing it with the one from the working python interpreter. In most cases I was running the script with a different python interpreter.
In my case , I was referring to wrong pip folder.
Changed virtual environment in pycharm to point to right pip folder to solve this issue
import sys
print ( sys.path )

Categories