Cleaning out all other installed pythons on OS X - python

For god's sake I'm going crazy. I'm on OS X mavericks. It seems that I installed a lot of pythons (Anaconda, EDA[something like that] ... ) and I just installed the one from Macports. Then also from Macports I installed a module for python called py-graph-tool. Now when I import this module in python I get this error:
Fatal Python error: PyThreadState_Get: no current thread Abort trap: 6
I found from this question the following: "this error shows up when a module tries to use a python library that is different than the one the interpreter uses, that is, when you mix two different pythons."
So I guess the solution would be to clean all other pythons. So my question is: how to clean all other pythons?

Assuming, you want to use the Anaconda Python for scientific work...do the following:
create a file called .bashrc in your home directory (~)
add the following to it: export PATH=~/anaconda/bin:$PATH
close out all open terminals
Now when you open a fresh terminal, python should bring up the Anaconda Python.

Related

Messed up Python directories

I'm a complete beginner to coding, Python, and anything beyond user-level computing. I've been learning to code with Python and everything was fine until I tried to import a module outside of Python's Standard Library (pyglet).
pip install pyglet worked and now returns:
> Requirement already satisfied: pyglet in
c:\users\nick\appdata\local\programs\python\python38-32 (1.5.7)
Then I received an error message when running the module in PyCharm.
>C:\Users\Nick\venv\Scripts\python.exe C:/Users/Nick/PycharmProjects/learning1/leaning1.GUI.py
Traceback (most recent call last):
File "C:/Users/Nick/PycharmProjects/learning1/leaning1.GUI.py", line 1, in <module>
import pyglet
ModuleNotFoundError: No module named 'pyglet'
Process finished with exit code 1
I think there are a few possible sources for the issue.
I think I might be saving files in multiple directories, so PyCharm is unable to find what I need. If that's the problem please direct me to a resource on learning how to organize files on my computer (something very basic about directories because I know nothing).
I installed python via python.org, but then Microsoft Store was being opened automatically when I tried to check my python version in the command line. I also downloaded Python from Windows Store.
I tried to solve my problems by completely uninstalling PyCharm, Python (windows store), and Python (python.org). I also tried to delete all previous .py files. I don't think I located everything.
I messed with the interpreter settings in PyCharm.
What I really want is to just start over from a clean slate by scrubbing all Python things!
There are a few steps you should take to prevent this sort of issue:
Use a virtual environment: steps to create a virtual environment
Verify that PyCharm's project interpreter is the virtual environment you just created (in my example, the project is named cheetah and the virtual environment is also named cheetah):

Importing python modules works in command line but not in the python 3.8 shell

I'm having a problem with importing modules in python.
When I run my program in the command line it works perfectly fine.
However, when I try to run the same program in the python shell I am prompted with the following error:
ModuleNotFoundError: No module named 'matplotlib'
I already successfully installed matplotlib using 'python -m pip install matplotlib'.
I've read this can happen when you have two different versions of python installed; however, I don't.
I've uninstalled and reinstalled python and I still am having the same issue. I've also uninstalled and reinstalled matplotlib using pip.
I believe my problem is the module paths that python uses to search for imported modules are different between the two.
When I use the 'print(sys.path)' command in the python shell and the command line I get two different outputs.
Any help would be greatly appreciated!!!
The file different system paths between the python shell and the command line
You have two versions of python. I would recommend you to remove all pythons you have and go for anaconda https://www.anaconda.com/distribution/. It will fix your path problems and allow you to create environments with different versions of python. This is the least painful way also for future :) good luck.
I suppose, you have both of the Python versions installed on the same computer.
If that is so, then my answer would be to go inside both Python script folders and install matplotlib on both of them.
I have also faced that issue. My path includes pip of Python 3.7.1 and whenevwer I try to import modules on Python 3.4. It throws an error!
Maybe, you could add both of the Pythons to the path.
I encountered this same problem – python -c "import sklearn" would work just fine, but import sklearn inside a Python program failed. Both my one-liner and program was using the same Python version (version 3.8.10).
I eventually got the program to work by replacing the shebang line (originally #!/usr/bin/python) with #!/bin/env python.
I don't know why this worked exactly (sorry). Presumably some path got reset, and the module loaded from a different location, but it might help someone so I'm posting it here nontheless. (If you know more, feel free to edit this answer.)

Issue with two versions of python

Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.

how to fix the "__path__ attribute not found" error for packages installed by pip installer?

I recently installed the opencv package using pip install and I wrote a small code to test it (cvtest.py). The code runs through the python idle shell but running it though the command prompt gives the error
Error while finding module specification for 'cvtest.py' (ModuleNotFoundError: __path__ attribute not found on 'cvtest' while trying to find 'cvtest.py')
I tried uninstalling and reinstalling both python and the package. looking up the system path using python -m site gives these results. I am the only user of my laptop.
sys.path = [
'C:\\Users\\Kareem Mostafa\\Desktop\\Assignments\\computer vision',
'G:\\Python37\\python37.zip',
'G:\\Python37\\DLLs',
'G:\\Python37\\lib',
'G:\\Python37',
'G:\\Python37\\lib\\site-packages',
This is the code I am using
import cv2
x=cv2.imread('backpack for sale.jpg',0)
cv2.imshow('x',x)
update: the problem is happening with all the py files I am having whether they require imports or not. apparently python is looking for _init_.py for all the files as if they are packages. Any idea what is going on?
For anyone else that had this problem (assuming kareemostafa has fixed it now!)
Removing the .py suffix on the python -m command fixes this problem, it appears -m only requires module names whereas running it directly as a python file (no -m option) requires the .py suffix
In your case python -m cvtest should be sufficient.

Python: Unresolved import error for sqlite3 in PyDev in Eclipse

import sqlite3 generates:
Unused import: sqlite3
Unresolved import: sqlite3
sqlite3 Found at: DatabaseTests
import sqlite3
However, this works perfectly in the terminal when using the python command-line.
I am running on a Mac Mountain Lion, with the default installation of Python. I am using PyDev in Eclipse Indigo.
This is a very old thread but I don't see the solution I found for this problem so I'll post it in hopes that somebody sees this and can then solve the problem:
you need to add 'sqlite3' (without the quotatios) in the 'forced builtins' tab in Window>Preferences>PyDev>Python Interpreter
I fixed this by adding _sqlite3 (note the underscore) to the Forced Built-ins in Window>Preferences>PyDev>Interpreters>Python Interpreter.
I have the same development environment and suffered from the same issue. I have resolved it by
switching to the MacPorts Python,
by only using packages that I install from that project
and by setting the default Python interpreter on Eclipse-Pydev to be the MacPorts executable
This works perfectly for me, with the one hitch that I still sometimes get an Unresolved import warning, but if the package is installed, my script still runs.
Looks like the project references do not have the site-package path added. Try adding the path to sqlite3 (/usr/lib/python2.7/sqlite3/ or the path installed to which you can get from python shell) to project references from Project -> Properties -> Pydev-PYTHOPATH -> External libraries.
I had a similar problem with other imports (simplejson and matplotlib).
I'm working on Ubuntu and the problems with the imports started after upgrading from Ubuntu 11.10 to 12.04 (which also upgraded the python version and the eclipse).
My steps for solving the problem were:
In the terminal, do 'import sys' then 'print sys.executable'. This gives the path to the current interpreter.
In Eclipse - Window->preferences->PyDev->interpreter-python. Remove the old path and add the path retrieved in stage 1.
In the problematic project go to Project->properties->PyDev-PYHTONPATH, the external libraries tab remove old paths and add the correct ones (on Ubuntu one can find it via the synaptic manager, e.g. for simplejson go to the synaptic manager, type simplejson in the search box, mark the right result right-click and choose properties from the menu then choose the 'installed files' tab to see the path for the installed files.
I hope someone will find it useful.

Categories