Difficulty importing packages to interpreter in Pycharm [duplicate] - python

In trying to install packages in Pycharm for the Python 3.6 interpreter, I get
Python packaging tools not found. *Install packaging tools*
And when I tried to install it gives me this error
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition
2017.3.1\helpers\packaging_tool.py", line 2, in <module>
import traceback
ModuleNotFoundError: No module named 'traceback'
I'm using Pycharm 2017.3.1 Community Edition

From pycharm,
goto settings -> project Interpreter
Click on + button on top right corner and you will get pop-up window of Available packages. Then search for packaging python package.
Then click on Install package to install the package.

I had a similar issue. In the end I had to remove an existing venv that was in the same location, delete multiple interpreters, then set up a new one.

For what it's worth..
I had a similar problem. Nothing answered here worked for me.
The problem started with Pycharm upgrade. The solution was to uninstall Pycharm, and obliterate what's in the Configuration directory (https://www.jetbrains.com/help/pycharm/directories-used-by-the-ide-to-store-settings-caches-plugins-and-logs.html#config-directory) along with alll jetbrains directories.
Reinstalled Phycharm 2022.1.1. Viola. Alas I have to reinstall all plugins I used in the old installation.
As far as I can tell, the problem - in my case at least - was the upgrade process.

The accepted answer wouldn't directly work if you have already created a Virtual environment.
That virtual environment would not be usable. So, go ahead and delete it.
Then, go and install the packaging module in the base python version, as shown in the accepted answer and then create a new virtual environment.
That new virtual environment can now be used.

Related

Trying to import python modules returns ModuleNotFound Error

Im attempting to use the numpy module in webots but whatever I try I always get
INFO: assignment_2_controller_2: Starting controller: python3.6 -u "assignment_2_controller_2.py"
[assignment_2_controller_2] Traceback (most recent call last):
[assignment_2_controller_2] File "assignment_2_controller_2.py", line 4, in <module>
[assignment_2_controller_2] import numpy
[assignment_2_controller_2] ModuleNotFoundError: No module named 'numpy'
WARNING: 'assignment_2_controller_2' controller exited with status: 1.
I have installed numpy with both pip2 and pip3 and neither seem to have made any difference and the package shows up in ~/.local/lib/python3.6/site-packages. I can also confirm that this module works in the python editor and in other python files.
Is there perhaps something I need to change in webots python Makefile or perhaps in my environment variables?
Edit:
Printing sys.path shows
'/snap/webots/14/usr/share/webots/lib/controller/python36', '/snap/webots/14/usr/lib/python36.zip', '/snap/webots/14/usr/lib/python3.6', '/snap/webots/14/usr/lib/python3.6/lib-dynload', '/snap/webots/14/usr/lib/python3/dist-packages'
I installed webots using snap but why wouldnt it use the python dictated by the PATH environment variable?
Fix:
By the looks of it snap creates a contained filesystem for webots to run in by design in which it installed its own python binaries. I therefore uninstalled webots with snap and then installed from the .deb file off their website and now it works fine.
Webots installed from the Snap package is running in a kind of sandbox, it is therefore using the version of Python distributed with the snap package (which you can't alter).
As explained in the documentation, you might use external controllers to launch yourself the controller instead of letting Webots starting it. This way you can chose which version of Python you want to use: https://cyberbotics.com/doc/guide/installation-procedure#installing-the-snap-package
Alternatively, you can use a different way to install Webots (from the Debian or archive package). The other installations are not coming with Python and are using the system Python.

Error importing Polygon from shapely.geometry.polygon

In my Anaconda 2.2 64bit with Python 3.4.3 the following line works well:
import shapely
But the following line:
from shapely.geometry.polygon import Polygon
returns the following error:
OSError: [WinError 126] The specified module could not be found
What am I missing?
EDIT
I tried with iNotebook, idle.exe and Eclipse. They all use Anaconda (the only Python installation on my computer) and they all show the same error.
If I type from shapely.geometry import Polygon in Eclipse, then I click on Polygon, then I press F3, Eclipse is able to open the module C:\Anaconda3\Lib\site-packages\shapely\geometry\polygon.py. So Eclipse is able to find it, but the execution fails.
EDIT 2
I just tried the same import on another computer with similar configuration and it works. The "only" difference between the two computers is that one has Windows 7 (it works) and one has Windows 8 (it doesn't).
I installed Anaconda and a few packages in both the computers following the same old checklist. The computer with Windows 8 can see the package from Eclipse, but can't import it.
I had a similar problem and it was due to the way that I improperly installed shapely (although this was on a Windows 7 machine, not Windows 8). For the initial install under which I couldn't import the geometry sub-module I installed shapely with the command:
pip install shapely
However after reading the documentation here more closely I saw that for Windows an executable installer needed to be used. That installer is a wheel file that needs to be launched by pip as well. So I uninstalled the first version of shapely with:
pip uninstall shapely
Then I launched the installer via the wheel file like this:
pip install your/file/path/Shapely‑1.5.9‑cp27‑none‑win32.whl
Note that you must download the wheel that matches the specs of the version of python that your applying the package to. I have 32-bit (win32) python 2.7 (p27) so the package above was the correct one. Note that the reference to 32 or 64 bit in the file name refers to the version of python, not to the version of windows.
Try with below one, it solved the issue for me (Windows 10):
conda install -c conda-forge shapely
shapely package docs
Try this:
from shapely.geometry import Polygon
That's how the docs list it:
http://toblerity.org/shapely/manual.html#polygons
Don't use anything but Python version 3.7. Download it now!
Then go to PyCharm (that's what I'm using) and open it, then download the file for your specific version (from here https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely) mine is Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl (this is respectively version 3.7 (37) and Windows (64) as seen above)
Now with your newly downloaded file, copy and paste it into where you are working from in PyCharm's folder location. For me this is: C:\Users\lewis\PycharmProjects\Project1\NewProject
Then you should see the newly copied file in PyCharm itself as well as where you put it.
Now right click the file in PyCharm Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl
and click OPEN IN TERMINAL then type:
pip install Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl
It should say "successful".
Then go into your working area and type:
from shapely.geometry import point
Run it. It should now work for all!
Be in mind that this will ONLY allow for the importing of Shapely in this one environment. So you'll have to copy the Shapely file into new folder locations if you use in other places.
Specs:
Be in mind I am working with a Windows 10, Python interpreter version 37, PyCharm, Virutalenv environment type. Also, make sure in the settings you are indeed in the Python 37 interpreter and not in a conda environment.

Using MacPorts to install modules via a certain path

I realize this is a very newbie question, but previous threads haven't been encouraging (see details below).
I've been using anaconda for most of my work via the IPython Notebook, python 2.7
However, there is a module which I could only properly download with MacPorts. Hence the current problem: when I try to use the Python command line in Terminal or use IPython notebooks, I will enter
import py-module
and I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py-module
So, obviously the $PATH is wrong. All my previous modules have been saved with conda to /anaconda/bin, but using sudo port install py-module is going to save in a different file, /opt/local.
Is it possible to set the path such that MacPorts will install a module into anaconda/bin
such that I can import this module on ipython notebook via conda?
Previous threads have suggested simply choosing one or the other, i.e. removing anaconda entirely
rm -r ~/anaconda
and then just reinstalling all packages via MacPort. But I would prefer not to do this. Can't I re-direct the path such that
sudo port install py-module
installs the module into `/anaconda/bin/' ?
For previous takes, see
Getting PyGame to import in Python interpreter after MacPorts install
While you could adjust your PYTHONPATH environment variable or modify the path using sys.path.insert, you should not try to mix a Python module built for MacPorts' Python with Anaconda. This may work just fine if the module is python-only, but as soon as the module contains compiled code it is not certain that the two installations are binary-compatible. In the best case, loading the module will fail if it isn't compatible, in the worst case, it will randomly crash.
So, yes, you should decide for one Python and just use that.

IPython kernel restarts after installing new module

I was using "ipython" with "qtconsole" (it works perfectly) after I install some new third-party module and the "notebook" it stops working and the kernel restarts continually.
the Error is here:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/IPython/kernel/__init__.py", line 10, in <module>
raise ImportError(__doc__)
ImportError: IPython.kernel has been replaced by IPython.parallel.
The previous version of IPython's parallel library was located at this
location (IPython.kernel). It has been moved to the IPython.parallel
subpackage and has been refactored to use zeromq/pyzmq instead of twisted.
Please see INSERT URL for further details.
[IPythonQtConsoleApp] WARNING | kernel restarted
how can I fix this issue?
the Error says "Please see INSERT URL for further details." where can I find this "INSERT URL" ???
thank you.
You have multiple versions of IPython installed, and it's getting confused. The version in /usr/lib is installed with apt, and old (pre-1.0).
The shortest workaround, if available to you, would be to apt-get remove ipython, leaving only one IPython on your system.
The most likely cause of this issue, since you say it was caused by installing another package, is easy_install. It is possible for easy_install to incorrectly add dist-packages to easy-install.pth when you already have a package installed with apt. To check this, look at the files (if they exist):
~/.local/lib/python2.7/site-packages/easy-install.pth
/usr/local/lib/python2.7/dist-packages/easy-install.pth
If there are any entries starting with /usr/lib/python2.7/dist-packages, they should be removed.

Incorrect PythonPath after editting bash_profile for Mac trying to install OpenCV

Total noob here trying to get OpenCV working on Python through Homebrew. I searched everywhere extensively and couldn't find an answer intelligible to me. 99% of what I have done for this install is blindly follow forum posts, though I am beginning to get an understanding of what all this means.
Installed software:
OS 10.7.4
Xcode 4.4.1
-Command Line Tools
HomeBrew
-Python
-GFortran
-OpenCV
1. write this on first line of .bash_profile and save and restart Mac
export PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"
2. open IDLE located at /usr/local/Cellar/python/2.7.3
.>>> import cv
Traceback (most recent call last):
File "", line 1, in
import cv
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cv.py", line 1, in
ImportError: No module named cv2.cv
3. Go to Terminal to check PATH and PYTHONPATH
myname-MacBook-Pro:~ myname$ set
.#edited out unnecessary lines
PATH=/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
PYTHONPATH=/usr/local/lib/python2.7/site-packages:
1) What does my PATH need to be? I know its comically out of whack from repeated attempts.
2) Why isnt IDLE using this PYTHONPATH?
3. And most importantly, WHAT DO I NEED TO DO OVERALL TO JUST GET AN ICON, ON MY DOCK, THAT I CAN CLICK, THAT OPENS A PROGRAM, WHERE I CAN CODE WONDERFUL PYTHON WITH MAGNIFICENT OPENCV.
Thank you very much for any help you can send my way. I have been attempting to solve this problem for the last 3 days and would be very very appreciative of any help.
So IDLE is a regluar application not a commandline program that you run from bash, so it's not going to read your .bash_profile or your environment variables like PYTHONPATH.
The good news is, there is a way to get IDLE to recognise new python modules which might be in unusual locations, they just need to be referenced from a place where IDLE expects them to be, which is the site-packages directory. The confusing part is that you probably have more than one version of Python installed since Python is installed by default in Mac OSX and you've also installed it with homebrew (and it looks like you've tried macports as well).
Based on the path in the error, we know that IDLE is looking for the module in /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ which is the homebrew installation.
It looks like you somehow have the cv module there or linked in the site-packages directory but you're missing some other necessary file(s).
I have not installed OpenCV myself, but from the instructions here: http://jjyap.wordpress.com/2014/05/24/installing-opencv-2-4-9-on-mac-osx-with-python-support/ It looks like you need to also symlink the opencv cv2.so file into your site-packages directory from the opencv files you got through homebrew. (note the path for your site-packages directory is different from the instructions in the link).

Categories

Resources