Anaconda / Pip ModuleNotFound Error when working on IDEs, python paths? - python-3.x

I'm teaching myself python and have been exclusively been using Jupyter Notebooks through Anaconda until now. I'm now trying to move away from Jupyter and write .py scripts in an IDE. While working in Jupyter i've pip installed modules and they've worked fine within Jupyter. I've just discovered though that if i do this through an IDE i get a ModuleNotFoundError.
I've looked online and there are alot of posts with similar issues but none that quite matches mine. While i'm learning actual python code ok there is a huge hole in my knowledge around the setup, the terminal etc, and when i first started out i downloaded different IDE's which may not be helping...
Here's a few things from posts that i've tried that may give a clue as to what's wrong.
1) which python gives me //anaconda3/bin/python
2) which pip gives me //anaconda3/bin/pip
3) in python in the terminal:
import sys
for p in sys.path:
print(p)
//anaconda3/lib/python37.zip
//anaconda3/lib/python3.7
//anaconda3/lib/python3.7/lib-dynload
//anaconda3/lib/python3.7/site-packages
//anaconda3/lib/python3.7/site-packages/aeosa
4) usr/local/bin/ has a bunch of files (not folders) in it like 'Python3.7', 'Python3.7-config' etc
5) which -a pip = //anaconda3/bin/pip
6) which -a python gives me two paths = //anaconda3/bin/python
/usr/bin/python
7) usr/bin/python is a unix executable file, when i click it it opens a python terminal that says Python 2.7.16. within the usr/bin directory all the other python related files seem to reference 2.7.
8) when i pip install i generally just go 'pip install x' at the terminal. I thought maybe i needed to do 'pip3 install x' it would maybe not point just to anaconda, but was a complete guess and while the modules downloaded it didn't help at all.
9) I tried using an alias alias python=/usr/local/bin/python3 - but as python3 isn't actually there, i just created another problem that i then had to 'unalias'
10) pip -V = pip 19.1.1 from //anaconda3/lib/python3.7/site-packages/pip (python 3.7)
11) pip freeze showed the all the modules i would expect to see
This post seemed similar: Modules are installed using pip on OSX but not found when importing
Any help would be greatly, greatly appreciated. I've been learning ok up until now but i'm pretty out of my depth with this side of things, and i can't move forward at all unless i find a way to use an IDE with third party modules...
UPDATE - Tried uninstalling and reinstalling anaconda and it made no difference. All still works in Jupyter, modules can't be found elsewhere from IDEs. Also can't seem to install things like pandas through anaconda as it already has it, so it doesn't seem related to the IDE not being able to find it –
Thanks

When using the Anaconda distribution you should avoid pip as much as possible. Use the conda package manager instead. Try installing missing packages from the conda-forge channel first:
conda install newpackage -c conda-forge
Before you can use Anaconda properly, you need to activate it:
conda activate
The Jupyter start link did this for you. The Anaconda Prompt does the same thing and you can start your IDE directly from there. However, all IDE's manage environments themselfs, which is way more flexible. So it is time well spent to learn your IDE's features first.

Related

ModuleNotFoundError: No module named '<module.name>' - Installation error

I am building a desktop app using Python and PySimpleGUI. So far, everything works just fine. Whilst I was working at the project, I realized I need to find a way to get the duration of some mp3 files and to display it in a certain way. I discovered mutagen module that is supposed to help me in this sense, I installed, and here the problem arise:
It throws me ModuleNotFoundError: No module named 'mutagen'.
Seeing this, I started to look for the problem, but I couldn't not understand why my interpretor did not find the module even though I Installed it CORRECTLY. (as PyCharm told me)
I have tried the following:
I am using a local virtual environment that has installed the dependecies for the project(and some extra) and I uninstalled and installed the package 3-4 times
I deleted the local virtual environment and I created another one. I installed the packages again and same issue.
I installed a random module (scipy) and I tried to import it somewhere in the project and it thrown me same error, but this time for scipy module
My guess is that I did not configured properly my interpreter, but to be honest, I have no idea what I am doing wrong, because I followed the same steps I've been using for creating a venv with its according interpreter and for other projects, it worked just fine.
Further details:
Using python3.9 base .exe
I installed the packages in two ways: one using the pycharm IDE, and one by running pip3 install mutagen
You may be using a different pip that is not the one that affects the Python you are using. Instead of using
pip install mutagen
Consider using pip as a module of the Python you are using:
python -m pip install mutagen
This way you'll be sure you are working on the same Python.
If you want to continue using plain pip, try which python and which pip to make sure they are referencing the same environment.

dyld: Library not loaded: /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation

vincens#VMAC: python3
dyld: Library not
loaded:/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
Referenced from:
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Reason: image not found
[1] 25278 abort python3
python3 env is not used when I update my Mac to the latest version. How can I solve it?
This worked for me with the same issue.
Check if you have multiple Python3.x versions installed. In my case I had Python3.6 and Python3.9 installed. brew uninstall python3 did not remove Python3.6 completely.
I was able to call Python3.9 from Terminal by explicitly running python3.9 instead of python3, which led me to believe the issue was caused by ambiguity in which Python3.x resource was to be used.
Manually deleted /Library/Frameworks/Python.framework/Versions/3.6 resulted in Python3 running as expected.
hint:
It may be sufficient to remove /Library/Frameworks/Python.framework/Versions/3.6 from your PATH environment variable.
Check in the location:
/usr/bin/python3
If you can invoke python3 interpreter from this location without any issues, copy this to
/usr/local/bin/python3
I upgraded to macOS Big Sur and had faced the same issue. I was not able to open Python interpreter from terminal and the Python version inside Library/Frameworks/Python.framework/Versions/3.6 was also being pointed to Python2.7.
Later, I found python3 in location /usr/bin/python3 working fine for me. I also tried creating a symlink for python3 to the above path but didn't work.
That's becuase you have installed both python 3.6 from system library & python3.9 from other source like brew and there are something wrong with the python in lower version. Please manually delete the python within /Library/Frameworks. sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.6 this command works for me.
I encountered this same problem on Step 1 of PyCharm's Create a Django project documentation.
I had previously set up Python on my Mac following these instructions. Can't promise this solution will work for everyone, but maybe if someone's Python was set up similar to mine the following solution that worked for me might help you...
Open Terminal
which python3
Copy the directory given to you... (For me it was /Users/foo/.pyenv/bar/python3)
When creating your new Django project in PyCharm (Professional Edition), set your base directory to the one received from which python3
Create your project
My case
Deleted Xcode (Maybe some folders are accidentally deleted like /Library/Frameworks/...
Also installed Python with anaconda.
My solution
Reinstall Xcode
Followed this to remove anaconda then everything works fine

What does wxPython need to run successfully?

I'm trying to get wxPython to work in a pyenv-based virtualenv with the virtualenv and virtualenvwrapper plugins on MacOS. I've read several questions about making this work, but most of the answers seem to assume that I'm using the system python version, and/or only address one aspect of a broken setup. I haven't yet seen anything that explains what wxPython is checking for when it starts.
I have python 3.7 compiled by pyenv with --enable-framework.
env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4
pyenv itself is installed in my homedir from a reasonably recent (within the last couple of weeks) pull from git.
To be clear, none of the tools or libraries in my Python toolchain are installed by Homebrew.
My virtualenv has access to the framework by virtue of --system-site-packages. Having access to the framework and to the display are supposedly all that's required for wxPython to work, yet I'm still getting the same error on start of any test app:
This program needs access to the screen. Please run with a
Framework build of python, and only when you are logged in
on the main display of your Mac.
It looks to me like everything I should need is available.
% pyenv which python3.7
/Users/matt/.pyenv/versions/3.7.4/bin/python3.7
% mkvirtualenv --system-site-packages --python python3.7 wxtest
Running virtualenv with interpreter /Users/matt/.pyenv/shims/python3.7
Already using interpreter /Users/matt/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/bin/python3.7
Using base prefix '/Users/matt/.pyenv/versions/3.7.4/Python.framework/Versions/3.7'
New python executable in /Users/matt/.ve/wxtest/bin/python3.7
Also creating executable in /Users/matt/.ve/wxtest/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /Users/matt/.ve/wxtest/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/matt/.ve/wxtest/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/matt/.ve/wxtest/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/matt/.ve/wxtest/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/matt/.ve/wxtest/bin/get_env_details
% python -m site
sys.path = [
'/Users/matt/devel/wxtest',
'/Users/matt/.ve/wxtest/lib/python37.zip',
'/Users/matt/.ve/wxtest/lib/python3.7',
'/Users/matt/.ve/wxtest/lib/python3.7/lib-dynload',
'/Users/matt/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7',
'/Users/matt/.ve/wxtest/lib/python3.7/site-packages',
'/Users/matt/.pyenv/versions/3.7.4/Python.framework/Versions/3.7/lib/python3.7/site-packages',
]
USER_BASE: '/Users/matt/.local' (exists)
USER_SITE: '/Users/matt/.local/lib/python3.7/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
% pip install wxPython
Collecting wxPython
[...]
Successfully installed numpy-1.18.0 pillow-6.2.1 six-1.13.0 wxPython-4.0.7.post2
The code for IsDisplayAvailable() is buried in the _core shared object library, so not particularly easy to examine. I'm not a C++ coder, and digging around in the code repository all I've been able to find so far is the docstring in src/_app.i, not the actual code.
On Mac OS X a False return value will mean that wx is not
able to access the window manager, which can happen if logged in
remotely or if running from the normal version of python instead
of the framework version, (i.e., pythonw.)
That list of requirements seem to be satisfied by what I have here. I don't have a pythonw binary, but as the pythonw(1) man page says:
Actually, since Python 2.5, the normal python also allows GUI access, so
python and pythonw are now interchangeable.
Does anyone have an exhaustive list of what wxPython actually expects to find before it runs?
The error you're encountering is likely to be a "brew-hole" (a.k.a homebrew installation/compatibility issue for wxpython v3 and sometimes v4).
To test some issues that might be at hand perform the following in your pyenv:
python --version
python3 --version
How are they installed? Did you use brew?
If brew or any non-anaconda: uninstall all.
Then : reinstall python via the anaconda 2019. (xx >04) packaged version and it gives you 3.7.4 or 3.7.5 depending on what you choose.
reinstall via conda/pip cmd-line the required packages that are not with default install.
Anaconda "base" is your default environment.
Then conda create --name myenv where myenv is any name you give your environment. For example "myPythonwx408" environment.
cmdline: conda activate myenv
... and tada.. up you go...
If the error persist in anaconda env you can post the environment.yml file so I can recreate your environment for testing.
You mention "virtualenv". Is that the actual virtualenv package or just what you are calling the pyenv instance? If the former then try python -m venv instead. The virtualenv package still hides the framework-ness of the parent Python, even after all these years. The venv package is included with Python3 and is a much better/simpler implementation of the concept.

Anaconda Breaks Python

Just installed Anaconda distribution and now any time I try to run python by double clicking a script, or executing it in the command prompt (I'm using windows 10) , it looks for libraries in the anaconda folder rather than my python folder, and then crashes. If I run via the command prompt, I'm able to see the error, which is:
File "C:\Users\bob\Anaconda3\lib\site-packages\pandas__init__.py",
line 19, in
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
I've uninstalled and re-installed Python and numpy multiple times, but it's getting installed in the default python folder, and since I installed the anaconda distribution, the python launcher always looks in the Anaconda folder. I have to run modules from IDLE or not at all.
Is there any way to get Anaconda to play nice with the standard python installation? I'd really like to be able to quickly and easily double click python scripts to run them.
Anaconda comes with pip , you should not need a separate installation. If you need a library not available in anaconda then you would use pip. Both will store your downloaded packages in site-packages. In your scripts folder you should have [usually only one] reference to python. Generally I think anaconda is the preferred repository. I would consider removing " standard installation". If you need two for some reason make sure only one is in your environment variable path. Assuming you don't have the anaconda version in your path; go to the scripts folder ,in the cmd prompt and type
python myscript.py

Upgrading Python 3 on Windows broke all my downloaded modules

I'm using Windows 7 and am up to date on patches. I was using Python 3.5.2 and wanted to upgrade to 3.6, so I went to the Python site, downloaded 3.6.4.exe for Windows, and ran it. The Python seems to work fine and is 3.6.4, but trying to import any of the packages I was using (pandas, numpy, tensorflow, etc) now gives me ModuleNotFoundError: No module named <whichever module it was>. Also, pip list now shows only pip and setuptools.
It appears to be a known issue, for example this from nearly a year ago, which suggests that I should "uninstall the python bindings and install again", but I can't figure out what that means. Fortunately I can still access 3.5.2 by using py -3.5, and then my imports work. Can anyone tell me how to fix this for 3.6 without having to reinstall all my modules manually?
I was thinking possibly I should have upgraded through pip; it seems like that's possible but maybe a bad idea for some reason? On further investigation it looks like what I should have done was save my requirements with pip freeze > requirements.txt, and then after installing the new Python restore them with pip install -r requirements.txt. Is this right?
Hard to say if you have an install problem, but this is what I would try if I were in your place.
Create a virtual environment as per; docs
c:>c:\Python36\python -m venv c:\path\to\myenv
Activate your virtual environment
C:> \Scripts\activate.bat
Run your application from within your activated environment. Each time you get an import error, do a pip-install from within the active environment. (For your own modules, you may need to modify PYTHONPATH in 'activate.bat')
Once you have your application running again, do your pip freeze > requirements.txt, and keep that with your project.
Each time you run your application, do so from within the activated virtual environment.
This will give you a clean requirements.txt that doesn't include a bunch of junk from other projects. Then, when you go to 3.7, just create the virtualenv and run your requirements.txt and wala!
I suspect your issue is simply not running against the correct interpreter, running from within a virtual environment should at least rule it out.

Resources