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.
Related
thanks for reading this. I am using macOS High Sierra. I am not very familiar with terminal or environment variables, but am trying to learn more. From reading other threads and google, it seems like I either have multiple pythons installed, or have pythons running from different paths. However I am not able to find a solution to resolving this, either by re-pathing my IDLE or deleting it entirely.
I do have python, python launcher, and anaconda (not very sure how anaconda works, have it installed a few years back and didn't touch it) installed. I am trying to install pandas (pip install pandas), which tells me that I have it installed, but when I run it on IDLE, it says module not found. Though if i run python3 on terminal and type my code in, it works (so pandas has indeed been installed).
When i run which python on terminal, it returns
/Users/myname/anaconda3/bin/python
(when i enter into this directory from terminal, it shows that in the bin folder, I have python, python.app, python3, python3-config, python3.7, python3.7-config, python3.7m, python3.7m-config)
When i run which idle on terminal, it returns
/usr/bin/idle (im not even sure how to find this directory from the terminal)
When i run import os; print(os.path) on IDLE, it returns module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py'
Would really appreciate some help to figure out how to ensure that when i install modules from terminal, it would be installed into the same python as the one IDLE is using. Also, I would like to know whether it is possible for me to work on VSCode instead of IDLE. I cant seem to find suitable extensions for data science and its related modules (like statsmodels, pandas etc). Thanks a lot!
First of all, a quick description of anaconda:
Anaconda is meant to help you manage multiple python "environments", each one potentially having its own python version and installed packages (with their own respective versions). This is really useful in cases where you would like multiple python versions for different tasks or when there is some conflict in versions of packages, required by other ones. By default, anaconda creates a "base" environment with a specific python version, IDLE and pip. Also, anaconda provides an improved way (with respect to pip) of installing and managing packages via the command conda install <package-name>.
For the rest, I will be using the word "vanilla" to refer to the python/installation that you manually set up, independent of anaconda.
Explanation of the problem:
Now, the problem arises since you also installed python independently. The details of the problem depend on how exactly you set up both python and anaconda, so I cannot tell you exactly what went wrong. Also, I am not an OSX user, so I have no idea how python is installed and what it downloads/sets alongside.
By your description however, it seems that the "vanilla" python installation did not overwrite neither your anaconda python nor anaconda's pip, but it did install IDLE and set it up to use this new python.
So right now, when you are downloading something via pip, only the python from anaconda is able to see that and not IDLE's python.
Possible solutions:
1. Quick fix:
Just run IDLE via /Users/myname/anaconda3/bin/idle3 every time. This one uses anaconda's python and should be able to see all packages installed via conda install of pip install (*). I get this is tiresome, but you don't have to delete anything. You can also set an "alias" in your ~/.bashrc file to make the command idle specifically linking you there. Let me know with a comment if you would like me to explain how to do that, as this answer will get too long and redundant.
2. Remove conda altogether (not recommended)
You can search google on how to uninstall anaconda along with everything that it has installed. What I do not know at this point is whether your "vanilla" python will become the default, whether you will need to also manually install pip again and whether there is the need to reinstall python in order for everything to work properly.
3. Remove your python "vanilla" installation and only use anaconda
Again, I do not know how python installation works in OSX, but it should be reasonably straightforward to uninstall it. The problem now is that probably you will not have a launcher for IDLE (since I am guessing anaconda doesn't provide one on OSX) but you will be able to use it via the terminal as described in 1..
4. Last resort:
If everything fails, simply uninstall both your vanilla python (which I presume will also uninstall IDLE) and anaconda which will uninstall its own python, pip and idle versions. The relevant documentation should not be difficult to follow. Then, reinstall whichever you want anew.
Finally:
When you solve your problems, any IDE you choose, being VScode (I haven't use that either), pycharm or something else, will probably be able to integrate with your installed python. There is no need to install a new python "bundle" with every IDE.
(*): Since you said that after typing pip install pandas your anaconda's python can import pandas while IDLE cannot, I am implying in my answer that pip is also the one that comes with anaconda. You can make sure this is the case by typing which pip which should point to an anaconda directory, probably /Users/myname/anaconda3/bin/pip
First: This would be a comment if I had enough reputation.
Second: I would just delete python. Everything. And reinstall it.
To repeat and summarized what has been said on various other question answers:
1a. 3rd party packages are installed for a particular python(3).exe binary.
1b. To install multiple packages to multiple binaries, see the option from python -m pip -h.
To find out which python binary is running, execute import sys; print(sys.executable).
3a. For 3rd party package xyz usually installed in some_python/Lib/site-packages, IDLE itself has nothing to do with whether import xyz works. It only matters whether xyz is installed for 'somepython' (see 1a).
3b. To run IDLE with 'somepython', run somepython -m idlelib in a terminal or console.
somepython can be a name recognized by the OS or a path to a python executable.
For my project I'd like to be able to dynamically load and use a Python 3 shared library, if one is installed on the system (any version >=3.2). If more than one Python 3 version is installed, it would be nice to know which one is the default (i.e. the one that gets used whenever user types python3 in the terminal).
On Windows, Python install locations can be discovered via the registry, however, I am uncertain if the same can be done on MacOS and Linux. While python3 executable can typically be found on PATH, there seems to be no general way of locating Python's shared library?
Edit: To clarify, I am looking for libpython3.x.so (or libpython3.x.dylib), not the main python executable.
Also, I'd like the discovery method to be portable: it should work on different Linux distros, and for different package managers on MacOS, so saying e.g. "It's in /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/Python" doesn't cut it.
In shell on MacOS or Linux, type:
which python and/or
which python3 to get your answer
Example on a VM on my machine (under Docker):
uwsgi#08c2ed391dae:/src/psf$ which python
/usr/bin/python
uwsgi#08c2ed391dae:/src/psf$ which python3
/usr/bin/python3
Have you tried :
'''
>>> import sys
>>> sys.path
'''
That shows lib path.
I am using PyCharm to develop a python project, which uses an external library called win10toast. I have installed win10toast using PyCharm. However, when I tried to run the .py file using cmd (i.e Externally running the python file), an error shows up:
ModuleNotFoundError: No module named 'win10toast'.
I have python 3.6.4. I installed win10toast using PyCharm.
from win10toast import ToastNotifier
I expect the program to run without any error, but currently I am getting the ModuleNotFound error.
Python can be tricky to run properly because it is sensitive to where you installed your dependencies (such as external libraries and packages). If you installed Python to one directory, but accidentally installed the external library to another directory, when you run your .py program, it will be unable to call from the external library because it doesn't exist in the same library that Python is running from.
Lookup where you installed Python on your computer and then find where you installed the external library. Once your find where you installed the external library, move its entire package content to the same directory where Python is installed. Or better yet, reinstall the external library with pip into the same directory as Python.
If you're on Mac, Python and its related dependencies are usually stored somewhere in /User/bin. If you're on Windows, it will be stored somewhere in your C:// directory (possibly somewhere in C:\Users\username\Local\AppData). If you're on Linux, it will be stored somewhere in /usr/bin. Whatever you do, don't move Python from wherever it is because sometimes that can mess up your system for certain operating systems like Mac, which comes with its own version of Python (Python 2.7 I believe, which is outdated anyway).
Lastly, you may have two different versions of Python on your computer, which is common; Python 2.7 and Python 3+. If you wrote your program in one version, but ran it from the other, the external library can only be called from whichever Python version you installed it to. Try running your .py program with python3 instead of python (or vice versa) and see what happens. If it works with one python version over the other, that tells you that the external library is installed in the other version's directory.
That should solve your issue.
I would suggest that you not use PyCharm to install packages, at least not
if the result deviates in the slightest from doing a "pip install" at the command line. I see no reason to involve PyCharm in configuring Python installations. It's just asking for trouble.
I admit that I'm not familiar with the practice I'm suggesting you avoid. I've been using PyCharm since pretty much the week it came out (was an avid user of the IntelliJ Python plugin before that), and have never once considered doing anything but installing Python modules at the command line. That way, I'm sure right where those modules are going (into which base Python install or venv). Also, I know I'm doing all that I can to minimize the differences that I might see between running code in PyCharm and running it at the command line. I'm making my suggestion based solely on this practice having never gone wrong for me.
I have multiple base Python versions installed, and dozens of venvs defined on top of those. PyCharm is great at allowing me to indicate which of these I want to apply to any project or Run/Debug configuration, and utilizing them seamlessly. But agin, I administer these environments at the command line exclusively.
I still experience issues in switching between the command line and PyCharm in terms of one module referencing others in a single source tree. My company has come up with a simple solution to this that insures that all of our Python scripts still run when moving away from PyCharm and its logic for maintaining the Python Path within a project. I've explained the mechanism before on S.O. I'd be happy to find that if anyone is interested.
The library win10toast installed in the directory: YOUR_PYCHARM_WORKSPACE\PycharmProjects\YOUR_PROJECT_NAME\venv\Lib\site-packages
but when you are running your program using cmd, pycharm interpreter uses site-packages directory that you installed python at there. for Ex: C:\Python27\Lib\site-packages
So, you can install the win10toast library to this windows directory using pip.
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.
My understanding was that as long as a non-Apple-default Python is employed to build, that the end-user need not install Python him/herself to execute a py2app-built app. In developing and testing the app in my own environment, I obviously have Python installed. Specifically, I built in a pyenv with with a python.org install, not Apple's own. Yet when I give the app to an end-user who doesn't have Python installed, she gets:
A Python runtime could not be located. You may need to install a
framework build of Python, or edit the PyRuntimeLocations array in
this application's Info.plist file.
The second line is concerning; if what it states is true, then a separate app instance would need to be built for every possible location of an end-user's install e.g /usr/bin, /Library/Frameworks etc.
UPDATE: Info.plist defines:
<key>PythonExecutable</key>
<string>/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python</string
Yet the end-user in question only has a system install in /usr/bin.
Does this mean that every end-user needs to have an externally-installed Python, and it must live in /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
What if they don't have a non-Apple Python? What if they have a non-Apple Python but it's not 2.6? How can this somewhat hardcoded dependency be avoided?
py2app automatically defaults to --semi-standalone mode if it thinks you are using the system interpreter. Your interpreter from Python.org shouldn't count as a "system" interpreter, but you could see what py2app thinks using this command:
$ python -c "import py2app.build_app; print py2app.build_app.is_system()"
False
One issue to watch out for: After I installed a Python.org interpreter today, bash didn't update it's hash cache, causing strange incompatibilities when I launched python. I had to type hash -r python to reset the cache and make sure the correct version of python was getting used. (Another way to fix this is to log out and log in again.) I suppose it's possible that the same issue could have caused py2app to be confused about whether or not you were using the system python.
If that doesn't do the trick, then try installing your python interpreter to a weird location, like ~/mypython or something like that, just to make sure there's no way it can be confused for a system python.
As a last resort, I suppose you could just hack the py2app source code so that is_system() always returns False. Not sure if that would have any adverse consequences, though.
PS -- Here's a little tutorial on using py2app with a conda-packaged application:
https://github.com/stuarteberg/helloworld Not exactly relevant to your problem here, but you could compare it with your own setup and look for any conspicuous differences.