Spyder not handling relative imports - spyder

I am attempting to python3 proof my work and spyder seems to be having an issue with absolute_import.
For demonstration purposes I created two simple files. caller and callme
caller
from __future__ import absolute_import
from .callme import helloWorld
def runme(msg):
helloWorld(msg)
if __name__ == "__main__":
runme('It worked!')
callme
def helloWorld(msg):
print("helloWorld's message is '{}'".format(msg))
if __name__ == "__main__":
helloWorld('Hi')
When attempting to run caller from spyder I get the following error:
ValueError: Attempted relative import in non-package
Running from ipython via the anaconda prompt (python 2) or from jupyter notebook (running python3 or python2) both work properly.
Ideas on how to fix spyder's behavior so it properly recognizes absolute_import?
Spyder versions tried:
3.2.4 Python 2.7.14 64bits, Qt 5.6.2, PyQt5 5.6 on Windows 10
3.3.2 Python 2.7.14 64-bit | Qt 5.6.2 | PyQt5 5.6 | Windows 10
Update
Updating spyder via conda update spyder (now version 3.3.2) did not fix the issue.

If you run python caller.py in a system terminal, you'll get exactly the same error as the one you posted, i.e.
ValueError: Attempted relative import in non-package
So this is not a problem with Spyder (because Spyder runs something similar to python caller.py when you execute a file with Run > Run), but with the way relative imports works.
Please see this answer for a proper explanation:
https://stackoverflow.com/a/11537218/438386
In essence, you can't use relative imports in scripts.
Note: There's a workaround to avoid this error, as described in this answer:
https://stackoverflow.com/a/11536794/438386
However, we don't have the ability to execute a script as a package in Spyder, sorry.

Related

No module named 'manipulation'

I am using Ubuntu 22.04 LTS via WSL and working on Python 3.10. When I try to run Drake tutorials locally with python3 -m pydrake.tutorials I get the following error: ERROR: the Jupyter notebook runtime is not installed! which is not the case since I already installed jupyter-notebook using sudo apt-get install jupyter-notebook and can easily open a jupyter notebook myself when typing jupyter notebook.
Additionally, I tried to copy-paste and run the following code from a tutorial:
import pydot
from IPython.display import HTML, SVG, display
from pydrake.all import (AddMultibodyPlantSceneGraph, DiagramBuilder,
FindResourceOrThrow, GenerateHtml,
InverseDynamicsController, MeshcatVisualizer,
MeshcatVisualizerParams, MultibodyPlant, Parser,
Simulator, StartMeshcat)
from manipulation import running_as_notebook
and i get the following error:
ModuleNotFoundError: No module named 'manipulation'
It seems as I don't have a module called manipulation except the one inside pydrake library. Is this a version issue?
I desired to mention it in case the two errors are interconnected.
None of the tutorials in Drake import anything from manipulation. The manipulation textbook repository examples absolutely do. You can find more about those here: https://manipulation.csail.mit.edu/drake.html .

Spyder IDE tells me my python version is incorrectly compiled (MAC)

I just downloaded Spyder IDE for my programming class. I had been using Replit for two months, so I figured this is high time I switched to Spyder.
The console shows this message:
"This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807"
NOTE: I had installed Python a year ago using Atom, but then today, before installing Spyder, I deleted Python and all files related to it. Then, I downloaded latest version of Python from Python's website (3.10.1).
I tried to find a work around for this by going to preferences, Python interpreter, and selecting the interpreter I downloaded, which is named Python IDLE. But, every time I try selecting that, it says invalid file path (/Applications/Python 3.10/IDL
I tried looking this up, but I cannot find something that is beginner-friendly. Can someone help me understand what is causing this?
I tried to add conda-forge as prioritized channel, and I created new environment to install python and spyder-kernel again.
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install
https://conda-forge.org/#about
I was thinking about clear my default 'base' environment, but that does not work well. Thus, I had to create new environment.
conda create --name myenv
https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands
After that I have to indicate new Python Interpreter on 'Preferences - Python Interpreter - Use the following Python Interpreter'.
Then, it works for now, but I have to install additional packages for my convenience later.
In conclusion, I can have Spyder application running through Rosetta 2 with Python Interpreter for Apple Silicon.
Python 3.10.2 | packaged by conda-forge | (main, Jan 14 2022, 08:04:21) [Clang 11.1.0 ]
Type "copyright", "credits" or "license" for more information.
IPython 8.0.1 -- An enhanced Interactive Python.
My answer might be so rough, so please let me know how can I improve.

pydrake is not available for installation anymore through google colab?

I have been using a google colab template for iterative LQR that uses the Pydrake, however, it seems like the code repository is removed and I can't reinstall it on google Colab:
try:
import pydrake
import underactuated
except ImportError:
!curl -s https://raw.githubusercontent.com/RussTedrake/underactuated/master/scripts/setup/jupyter_setup.py > jupyter_setup.py
from jupyter_setup import setup_underactuated
setup_underactuated()
# Setup matplotlib backend (to notebook, if possible, or inline).
from underactuated.jupyter import setup_matplotlib_backend
plt_is_interactive = setup_matplotlib_backend()
File "/content/jupyter_setup.py", line 1
404: Not Found
^
SyntaxError: invalid syntax
I tried clicking this link https://raw.githubusercontent.com/RussTedrake/underactuated/master/scripts/setup/jupyter_setup.py, and the page is not found... everything was working fine yesterday
Sorry. You're correct... I updated it this morning, and don't have a good deprecation policy in place on that repo, and this setup script is two versions ago. The path you want is https://raw.githubusercontent.com/RussTedrake/underactuated/master/setup/jupyter_setup.py
(remove the script from the directory). But if you look at that file, you'll see that even that is pointing to an updated setup script which you might want to point to.
This is actually all good news... we are on the path to a much better solution. You can now just pip install drake on colab (see the drake installation guide). Once I land the pip install underactuated (probably in time for my Spring offering of the class), then all of that nasty setup will be gone.

Downloaded module not found in IDLE

I am trying to use some libraries I downloaded, but whenever I check for them in IDLE I don't see them.
The same thing happens when I type from sklearn import datasets
But when I try to pip install I see this:
Does this mean I am limited to only the Spyder IDE?
There are somehow distinct distributions of Python that you seem to be running. I would suggest using virtualenv and running your IDE (idle/others) via that. Here's a tutorial video for it. [1]
[1] https://teamtreehouse.com/library/idle-in-a-virtual-environment-2

python-oauth2 on python 3x and windows

I want to setup an oauth library for Python on my windows desktop. I am newbie and this is my second day on Python and I having a tonne of trouble.
I downloaded the python-oauth2 (hudson-python-oauth2-167.zip) from github. I have extracted this to my python32 folder. When I run the setup command "python setup.py", I first got a syntax error on the print statement. I assumed it is because I am running on windows and so I changed it and then ran the setup.
I then got the following error:
Traceback (most recent call last):
File "C:\Python32\simplegeo-python-oauth2-1920657\setup.py", line 2, in
from setuptools import setup, find_packages
ImportError: No module named setuptools
Can someone guide me with setting up python-oauth2? Am I missing something basic here?
You need to install Distribute, which is a fork of setuptools that supports Python 3.
That said, your syntax error was because python-oauth2 doesn't seem to run on Python 3 yet, so you need to either help port it, or use Python 2.7.

Resources