Numpy fails in Pycharm but runs on VSCode/terminal M1 Mac - python

My code uses matplotlib which requires numpy. I'm using pipenv as my environment. When I run the code through my terminal and pipenv shell, it executes without a problem. I've just installed Pycharm for Apple silicon (I have an M1) and set up my interpreter to use the same pipenv environment that I configured earlier. However, when I try to run it through Pycharm (even the terminal in pycharm), it throws me the following error:
Original error was: dlopen(/Users/s/.local/share/virtualenvs/CS_156-UWxYg3KY/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 0x0002): tried: '/Users/s/.local/share/virtualenvs/CS_156-UWxYg3KY/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
What's confusing me is the fact that my code executes when using this same environment through the my terminal... But it fails when running on Pycharm? Any insights appreciated!

Macs with m1 chips have something called Rosetta. This is basically simualting the architecture of Intel and Amd chips. When you click on the properties of your terminal there should be an option to open the terminal with Rosetta.
I had the same problem already, in each IDE you can set if the script should be run with imulatation (Rosetta) or without. So you should look up how to tell pycharm to use Rosetta.
Set the "shell path" under preferences - tools - terminal:
env /usr/bin/arch -x86_64 /bin/zsh --login
that should fix it

The problem ended up being the python version I had installed. Since I upgraded from intel to M1, the python version also carried over. Downloading a new 3.10 version and creating a new interpreter for it solved this issue.

Related

zsh: killed python3 on M1 MacBook Pro

I'm trying to run Python 3 on the new M1 chip MacBook. I tried installing python3 both via the official Python website (https://www.python.org/downloads/release/python-391/) and also via brew install python3. Both of the installations succeeded, however when I try running the command python3 in Terminal I get this error:
zsh: killed python3
Does anyone know what's going on? I couldn't find anything online and I think my installation should be fine. I did have to change my ~/.zshrc file for a class, but I'm not sure if that's the issue?
I faced this same issue in the M1 Macbook pro and fixed it by doing the following
Open ~/.zshrc file (Create a new one if not present already)
Add the following line
alias python3="/usr/bin/python3"
Make sure you are able to access python3 from the above location by running
/usr/bin/python3
This should invoke Python3 for you or check for the correct path in /usr/bin and update the above line accordingly. You can do the same for other versions of Python also accordingly.
Try reinstalling python and all dependencies using homebrew!
brew reinstall $(brew deps python3) python3
I started to face the same issue myself while on M1 chip MacBook with macOS Monterey. I was using VS code to try to run my python script and it worked fine. But when I tried it on the terminal I kept getting that same response.
The simple solution that I found to work was specifying which python version I was running. Ex: say I have a file named testing.py and I am running the latest version of python 3.X, then in my terminal I would do
python3.X testing.py
and everything seemed to run just fine (at least for me).
Enjoy
I got a same error. Then I disabled MacOS System Integrity Protection(SIP), python3 runs correctly.

Visual Code on Windows can't find module after installing it with PIP. Using wrong Python in Terminal window?

I am using Visual Code 1.47.0 on Windows 8.1 with the latest version of the Python extension installed too. I just installed a module using pip. I then created a new Python file to test it. The IDE highlights the import statement as an error and the drop-down info box says "Unable to import 'extpylib'. If I try to run/debug the test file, I get a traceback error saying:
C:\Users\User\AppData\Roaming\Python\Python38\Scripts
Exception has occurred: ModuleNotFoundError
No module named 'extpylib'
File "C:\Users\user\Documents\PythonProjects\test-extpylib.py", line 2, in <module>
import extpylib
If I run the pip install command again, even with the "--upgrade" command line argument, it says everything is installed and that I'm using the latest build. I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit? I am not using the Code Runner extension.
I assumeg that this mismatch of versions between the IDE and the VSCODE Terminal window is causing my issue? If so, how can I get the VSCODE Terminal window to use the same version as that which is selected in the IDE? If not, what is the real problem and how can I fix it?
You may have many python paths and VS code is using the wrong one. Choose the right interpreter path just clicking here:
I solved this by closing and re-opening the vs-code after installing new package.
It seems like an environment issue. In my own Visual Studio code integrated terminal, when I type which python and which pip, I get the correct path to both python and pip executables. So this:
I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit?
Definitely seems to be the issue.
If your VSCODE terminal still uses Python 2.7, you could follow the tips presented here:
However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.
and
Note: conda environments cannot be automatically activated in the integrated terminal if PowerShell is set as the integrated shell. See Integrated terminal - Configuration for how to change the shell.
and
Changing interpreters with the Python: Select Interpreter command doesn't affect terminal panels that are already open.
Of course, the package you installed in an environment is independent of other environments.
As you said the VSCode Terminal using Python2.7 while the status bar shows the selected interpreter is Python3.8.3 64-bit. This meaning you are using the global python(3.8.3) to run the python file while you install the package to the Python(2.7) environment.
Solution:
After you selected the python interpreter in the status bar, you need to activate the environment in Terminal through shortcut 'Ctrl+Shift+`'. You can through these commands to check which environment you are using:
In the terminal:
By command "pip --version" to check which pip you are using.
By command "python" -> "import sys; sys.executable" to check which python you are using.
By command "python" -> "import sys; sys.path" to check which 'site-package' the interpreter searching for.
pip command that was used to install the package is based on a different version of python versus what VS Code is using.
Check the python version of your pip command, run pip --version. For example, on my system, this is the output of this command.
$ pip --version
pip 22.0.4 from /Library/Python/3.8/site-packages/pip (python 3.8)
Check the version of code that VS code is using. For VS code 1.61, the python version will be visible at the bottom left corner while a python file is open in the editor. Screen shot of the same is given below. You can also observe from the screen shot that lazy_streams package is not being recognised by VS code.
To solve the error in VS code, update the python version in VScode to be same as that of pip. Updating of the version can be done by simply clicking on the Python version displayed, which will further prompt you to select among all available python versions on your system.

"r.start is not a function" Fermipy Conda Error

I've been using the Fermipy conda environment on Python 2.7.14 64-bit on macOS Catalina 10.15.5 and overnight received the error "r.start is not a function" when trying to connect to the Jyputer server through Vscode (if I try on Jupyter Notebook/Lab the server instantly dies). I had a bunch of clutter on my system so I ended up formatting it and reinstalling all the dependencies needed (such as Conda through Homebrew, Fermitools through Conda and Fermipy through the install script on their site), but still get the same error, although I was previously running python scripts just fine. It gives me no other error or output, if it did I would attach it here. This is the error I get.
Edit: I get the same error using any version of Python 2.7.XX and not for python 3.7.XX.
As answered here, https://github.com/microsoft/vscode-python/issues/12355#issuecomment-652515770
VSCode changed how it launches jupyter kernels, and the new method is incompatible with python 2.7.
Add this line to your VSCode settings.json file and restart.
"python.experiments.optOutFrom": ["LocalZMQKernel - experiment"]
I got the same message. (r.start is not a function.) I had an old uninstalled version of anaconda on the computer which had left behind a folder containing its python version. Jupyter was supposed to be running from new venv after setting both python and jupyter path in vscode. I fully deleted remaining files from old anaconda install - message went away and notebook ran fine. Maybe try getting rid of all conda stuff and pip install jupyter and anything else you need.

Error using python with terminal after installing python 3.6 using macports

I use macports to install packages on my laptop. I have used it to set up and install a python environment (version 2.7). Upon installing python27 and using macports to set python the command to point to the python27 I am able, in the terminal to type:
:~ python
which correctly opens a python environment in my terminal session, where I can execute python commands.
When I follow the same process using macports to install a python 3.6 environment and type the command:
:~ python3
The environment loads correctly, however if I try to run any command in this environment, such as:
>>> print('Hello')
I get the following error:
>>> print('Hello')
Python(24202,0x7fffc0d003c0) malloc: *** error for object 0x10a78f110:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
This only occurs when I use the terminal python environment. For all intents and purposes, the install runs fine and I am able to run python 3 programs on my system and things like IPython work fine. This is a specific problem for when I enter the python3 terminal environment after installing it using macports.
I would like to know, what causes this to happen and is there a way I can fix it?
I have tried commands like:
port diagnose
and removing the port, all related ports and reinstalling but I am presented with the exact same error. Does anyone else experience this or have a resolution for it?
Please check out this link #48807, it is a known issue and below lines are quoted from the thread:
Just installed python36 and the install recommend py36-readline and had malloc crashes along the lines of pointer being freed was not allocated python.
Found this thread. Uninstalled py36-readline and installed python36 +readline, which seems to have fixed this.

Force PyCharm (1.5.4) configured interpreter to use 32-bit OSX Lion Python distribution (2.7.1)

I know how to start PyCharm in 32-bit mode on OSX Lion, but how do I get the interpreter configured in PyCharm to use the 32-bit version of the Apple shipped Python version (currently 2.7.1)?
I successfully have it working when launched from the terminal, but it appears that PyCharm doesn't read those system variables or defaults.
I'm trying to get cx_Oracle working with some scripts in PyCharm. Please see the following question for more details:
Can't get cx_Oracle to work with Python version 2.7 / mac os 10.7.2 (Lion) - missing_OCIAttrGet
Thanks in advance for your response!
I don't use PyCharm so I can't test this but it appears you can configure a non-standard path to the Python interpreter (see PYCharm help here). If so, try using /usr/bin/python as the path. If you've used the defaults command to permanently set 32-mode (as documented in Apple's man python):
defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
that should do the trick. Setting the environment variable probably won't work.
UPDATE: Since you report that that does not work, here's another, more drastic possibility. You can extract the 32-bit architecture binary from the multi-architecture (universal) binary by using the lipo command. Try something like this:
sudo lipo /usr/bin/python2.7 -extract_family i386 -output /usr/local/bin/python2.7-32
sudo chmod 755 /usr/local/bin/python2.7-32
Then set the interpreter path in PyCharm to that file. It's ugly because you will need to keep an eye on any Python updates from Apple and repeat the process. If PyCharm is exec-ing the Python executable directly from the framework, then this may not work. Short of getting some support in PyCharm or resolving the Oracle issue, the fool-proof solution would be to install a 32-bit-only version of Python. The pre-built 32-bit-only installers from python.org are problematic for Lion 10.7 because of their dependence on gcc-4.0 and the 10.4u SDK, both no longer provided in Xcode 4. However, you could build it yourself or, with a little bit of configuring, you should be able to get MacPorts to build one.
For some reason none of that worked for me. Kind of lame that pycharm doesn't support this..
I ended up adding the BASH support plugin to PyCharm: Preferences > Plugins > Browse Repository > BashSupport
Then I add a new bash file to my project with the content:
#!/bin/bash
arch -i386 /usr/bin/python ./<your script name that's in the same directory here>
Run it by right clicking and running. Now it'll appear in your 'configurations' drop down.
Now you can run the script as 32-bit python, see the standard out, and edit the .py file.

Categories