How to use Vpython (visual) from an IDE - python

I'm very new to python and I don't entirely understand versions and libraries, so there may be a simple solution to my problem.
I am working on a project in which it is vital that we visually display our simulation. I installed Vpython on Mac OSX 10.8.4 and so far visualizations work when running the program from the command line. However I'd like to use an IDE (Eclipse, Sublime Text, Spyder. Ideally Spyder) but whenever from visual import * shows up, I get the following error:
>>> runfile(r'/Users/robinnewhouse/code/Spyder/astro/maintest.py', wdir=r'/Users/robinnewhouse/code/Spyder/astro')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 523, in runfile
execfile(filename, namespace)
File "/Users/robinnewhouse/code/Spyder/astro/maintest.py", line 7, in <module>
from visual import *
ImportError: No module named visual
So I tried using Spyder's PYTHONPATH manager to add the path to the package. I got the same error.
Then in a last ditch effort, I tried to manually add the "visual" package to Spyder's directory of included packages (/Applications/Spyder.app/Contents/Resources/lib/python2.7/) and I got a different (albeit worse) error: Fatal Python error: PyThreadState_Get: no current thread
I have no idea how to deal with this issue, but the most frustrating part is that if I run the same file in command line, it works fine. (speaking of which, if it helps, the version of python that comes up when I use which python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Thanks for any advice you can offer.

(Spyder dev here) The right solution is this: In Spyder you need to go
Tools > Preferences > Console > Advanced Settings > Python Executable
and then change the file that appears there to be
/usr/bin/python
However, before doing this you need to reinstall Spyder again, because you added the visual module to it and now it's broken.

Related

Pycharm: DLL load failed: The specified procedure could not be found

I am working on a Python project in Pycharm (2020.1.2) on Windows 10.
For this project, I cannot use the standard Python interpreter, I have to use my own located at C:\some\path\here\python\27_64\python.exe (Python 2.7.3).
Backstory may be important:
I have added this path to the system path for both myself and all users, and placed it ahead of %LOCALAPPDATA%\Microsoft\WindowsApps to try to prevent the Microsoft store from popping up whenever I try to run python on the command line - however I don't feel like this change to the path variable has made a difference, as the Microsoft store still pops up.
I can start a Python shell by running C:\some\path\here\python\27_64\python.exe, so I know it technically works. When I do so; the sys.path is as follows:
['', 'C:\\another_place\\Python_2.7.3_x64\\python27.zip',
'C:\\some\\path\\here\\python\\27_64\\DLLs',
'C:\\some\\path\\here\\python\\27_64\\lib',
'C:\\some\\path\\here\\python\\27_64\\lib\\plat-win',
'C:\\some\\path\\here\\python\\27_64\\lib\\lib-tk',
'C:\\some\\path\\here\\python\\27_64',
'C:\\some\\path\\here\\python\\27_64\\lib\\site-packages']
Anyway, when I try to run a Python console (not even my script), this is the message I get in Pycharm:
C:\some\path\here\python\27_64\python.exe "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py" --mode=client --port=59771
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py", line 5, in <module>
from _pydev_comm.pydev_rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_comm\pydev_rpc.py", line 1, in <module>
import socket
File "C:\some\path\here\python\27_64\lib\socket.py", line 47, in <module>
import _socket
ImportError: DLL load failed: The specified procedure could not be found.
Process finished with exit code 1
I have gone to Settings>Project:[name]>Project Interpreter and set it to C:\some\path\here\python\27_64\python.exe (and rebooted Pycharm to be sure). That said; in the settings window no packages are shown and it claims that Python packaging tools can not be found.
When I click the link to install them (circled in red), they can not be installed due to this error:
ImportError: cannot import name _remove_dead_weakref
Are there other variables or settings I need to change?
Thanks
EDIT
Uninstalling the first Python on my Path (C:\\another_place\\Python_2.7.3_x64\\python27.zip) just makes everything so much worse
EDIT 2
I added the PATH variable manually to both the Python console settings and to the Run/Debug settings in PyCharm (and restarted the program), the result is still the same
I had a similar issue. This procedure fixed my issue.
Try the following:
run print(os.environ['PATH']) in the system terminal using the same interpreter
copy the result and add as PATH environment variable to your Run/Debug Configuration
do the same for Python Console settings
I hope it will work.
It seems that the interpreter is not being recognized by the windows as a result of which you are unable to install packages.
Also, I suppose the interpreter should be present in the bin folder of your python folder. The interpreter does not have a .exe extension.
I would suggest installing anaconda python 2.7 64 Bit windows package installer and using the condo environment and work on python 2.7
Here is a link I found for Python 2.7 on Windows hope this helps:
https://docs.python.org/2/faq/windows.html
Had the similar issue, in my case it was always trying to find libraries in PostgreSQL installation directory.
Mentioning sys.path helped me here! I tried printing it from inside my script and realized that PostgreSQL directories appear earlier in the list than Python directories.
SO, how I finally fixed it -- added PYTHONPATH environment variable to my Run configuration in PyCharm like this (replace with paths to your Python installation directory):
PYTHONPATH=D:\PROGRAMS\Python\Python3.9\DLLs\;D:\PROGRAMS\Python\Python3.9\lib\;D:\PROGRAMS\Python\Python3.9\;D:\PROGRAMS\Python\Python3.9\lib\site-packages
This helps to put desired directories at the beginning of the list, thus they are searched first and required libraries are found as it is supposed to work.

Pycharm Startup will not connect to console

I am trying to start up pycharm and am getting the following error while connecting to the console:
C:\Users\i_am_yohan\Anaconda3\pythonw.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3\plugins\python-ce\helpers\pydev\pydevconsole.py" --mode=client --port=52061
Failed to import the site module
Traceback (most recent call last):
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\site.py", line 579, in <module>
main()
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\site.py", line 566, in main
known_paths = addsitepackages(known_paths)
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\site.py", line 349, in addsitepackages
addsitedir(sitedir, known_paths)
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\site.py", line 207, in addsitedir
addpackage(sitedir, name, known_paths)
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\site.py", line 159, in addpackage
f = open(fullname, "r")
File "C:\Users\i_am_yohan\Anaconda3\envs\Base\Lib\_bootlocale.py", line 12, in getpreferredencoding
if sys.flags.utf8_mode:
AttributeError: 'sys.flags' object has no attribute 'utf8_mode'
Process finished with exit code 1
I am making a transition from spyder to pycharm and am currently experiencing a lot of problems most notable this one. I have set up the correct project interpreter.
I get the same error, after I uninstalled Python 3.6, which I did after installing Python 3.7. I was already using Python 3.7 through Pycharm successfully for a while, but realized after some time that I had Python 3.6 still installed, without using it. Doing the following fixed this for me.
Before trying anything else, you might want to check that you are using the expected Python interpreter for what you are trying to execute.
I don't know what exactly has changed that fixed this, and I wouldn't necessarily recommend this solution, since my understanding of what really happened is very limited. But it doesn't seem to have broken my project, and so maybe someone is helped by this.
I cut and pasted my .idea file from my project root folder. Note that I cut and paste it, I didn't delete it. Removing this folder makes you lose a many, if not all, of your Pycharm project's settings. So don't delete this folder unless you want to lose your project settings, like your run/debug configurations.
Then I restarted Pycharm, and it automically started to create a new .idea folder (which can be very time consuming). I closed Pycharm again, since I realized I didn't want to go through with this, I had too many settings that I didn't want to lose, and so I removed the newly created .idea folder, and put back the original .idea folder.
After starting up Pycharm again, all the time in the same project folder, Pycharm did have to do some indexing, but then it worked. The project I am working on is a Django project, and I have a run configuration for Django. The error was thrown when I wanted to run the Django configuration (Maybe I should have checked the interpreter is was set to before doing all this). The only thing I was required to do to after the swapping back and forth of the .idea folder was change the Python interpreter for the Django configuration, it seemed to be reset.
I have figured this out. I should have connected to:
C:\Users\i_am_yohan\Anaconda3\python.exe
rather than:
C:\Users\i_am_yohan\Anaconda3\pythonw.exe
I got confused because I was moving from Spyder to Pycharm and Spyder is connected to pythonw.exe rather than python.exe
This happends when python console wants to use Python 3.7 interpreter to connect to a project build based on previous version of python. Here is a quick solution if you have installed Python 3.7 correctlly.
Go to your Pycharm, find 'Switch project interpreter' and change the interpreter into the Python 3.7 version.

Python SSL Import Error in PyInstaller generated executable

I'm trying to distribute an executable file of a Python program I've created.
For this purpose I'm using PyInstaller .
I use Windows 7 Professional with Python 3.7
The program runs successfully when inside PyCharm and produces the expected results.
As described in the PyInstaller manuals I've created the dist folder with:
pyinstaller main.py
The log output specifies that the process finished successfully.
When trying to run the main.exe from the distribution folder, the below error is produced:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 11, i
n <module>
File "c:\users\tim\anaconda3\envs\timtf2_37\lib\site-packages\PyInstaller\load
er\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified procedure could not be found.
[6236] Failed to execute script pyi_rth_certifi
So after a couple of hours, I managed to solve the problem.
I did several things and still unsure what was the actuall fix, but this is the step-by-step flow I've done:
Created a new environment using Anaconda. I've downgraded the Python version to 3.6.8, since I saw mentions of several compatibility problems with Python 3.7
Installed all the requirements from my project, using Anaconda, since some mention problems due to mixed usage of pip and conda.
Installed the latest version of pyinstaller.
Activated the new environment and run pyinstaller from within it on the main.py script of the program with --hidden-import pyodbc argument.
That's it, now the distibuted program works on all Windows computers in the office.

How to make executable Python file from .ipynb (Jupyter Notebook)?

I wrote a Python program in Jupyter Notebook. The program uses libraries installed through Anaconda. I need to get a separate executable Python file that would work on forks of Ubuntu and Debian.
I created the .py file from the .ipynb file through the menu in the Jupyter Notebook:
File -> Download as -> Python (.py).
Next, I try to run .py file through the Terminal in Linux:
>>> python3 name_of_created_file.py
And I get an error:
Traceback (most recent call last):
File "name_of_created_file.py", line 11, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
As I understand it, there are not enough software libraries to run the program. On the same computer (Linux) in Jupyter Notebook, my program works well.
How to get a working program separately from Jupiter Notebook? To do this, I need to separately install software libraries?
Can you please check the version of python in your local, whether python or python3. Your issue might be because of two different python versions.
Depending upon your version of python in local machine, you might have to use the same version of pip to install pandas.
Check the version of python you are running in Jupyter.

Multiple programs installing python modules on computer, confusing the crap out of IDLE

Most of the programs I use for my work have independent python API's which do various things. The downside to this is that it appears that each program installs its packages wherever it wants. This is by way of introduction.
Now, I am trying to run matplotlib through IDLE. I ran a clean pip upgrade and the ran a clean install of matplotlib using pip in cmd. The commands I used were found in this document, page 13.
When I try to import matplotlib in IDLE, I get the following error message:
>>> import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
File "C:\Program Files\ParaView 5.2.0-Qt4-OpenGL2-Windows-64bit\bin\Lib\site-packages\matplotlib\__init__.py", line 727
except Exception, msg:
The path it points to is for an independent program named Paraview which has its own set of python modules. It looks like Paraview also has a definition for matplotlib but I don't want to use this one. A quick snip of the paths in IDLE shows:
It looks like IDLE is prioritizing the Paraview folder over the others. How do I get IDLE to disregard this folder and look into the matplotlib module I just installed by default? Also how do I prevent this unfortunate and seemingly unnecessary duplication of modules?

Categories

Resources