Python modules installed via Homebrew not importing in .command executable - python-3.x

New Python programmer here! ๐Ÿ˜Š Following the answer, I tried to make an executable .py file on MacOS following these steps:
Installed Homebrew via MacOS Terminal ($ /usr/bin/ruby-e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)")
Installed Python3 from Homebrew (brew install python3)
Installed required modules (e.g. python3.7 -m pip install pyfiglet)
Then, I created a simple hello_world.py file:
#!/usr/bin/env python
python import pyfiglet
print(pyfiglet.figlet_format("Hello world!"))
input("Press any key to exit.")
Next, following these instructions,
I renamed the file from hello_world.py to hello_world.command
From terminal, I entered $ cd Desktop/python to my python projects folder
Granted permission chmod +x hello_world.command to the file.
This made the python script executable, and double-clicking it opened a terminal window. However, I get the traceback ImportError: No module named pyfiglet.
I have confirmed pyfiglet is installed and works successfully when I paste the code directly into terminal using python3.7. Anyone know why the .command executable doesn't work? I followed these steps to the letter as described in this answer. Thanks for your help!

Related

pip in path but command not found

I'm running a fresh installed OctoPi image (0.18.0). I added pip to my PATH in .bashrc file and sourced it but when I try to run pip I'm getting -bash: pip: command not found error.
When I run /home/pi/oprint/bin/pip, pip is working properly.
My path:
pi#octopi:~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/oprint/bin/pip:
You added the full path of the executable to the path, but you're only supposed to add the directory of the executable. Every exectuable of said directory is then directly available without the directory prefix.
In other words .. instead of /home/pi/oprint/bin/pip you should add /home/pi/oprint/bin/ to the path.
I always recommend using the Python executable to locate the pip executable for you. This way, you know the right version of pip is being used to install packages that are compliant with your Python version.
$ python -m pip install <the_package_you_want>
It is really easy to end up with mismatched versions otherwise!

Python/Tkinter : ModuleNotFoundError: No module named '_tkinter'

This is my first post StackOverflow, I will try to make it as correct and complete as possible if you have any tips to improve my post I will gladly accept it.
I'm having trouble running code written in Python that uses Tkinter.
I will try to describe in detail my actions to facilitate the identification of the error.
I started a course at Coursera on DSP (Digital Signal Processing) where it is suggested to install a tool written in python (and a little bit of C). I'm using Arch Linux.
link on Github:
sms-tools repo
Using pyenv/virtualenv/virtualenvwrapper I created an environment with Python 3.7.5, as recommended in the "How to use" section of the repository.
I installed the required libraries in my environment by pip:
%pip install ipython numpy matplotlib scipy cython
I compiled some C functions in the "/sms-tools/software/models/utilFunctions_C"
directory with the following command:
%python compileModule.py build_ext --inplace
Finally, I run the models GUI in the directory "/sms-tools/software/models_interface"
%python models_GUI.py
and I get the following message:
Traceback (most recent call last):
File "models_GUI.py", line 6, in <module>
from Tkinter import * ## notice capitalized T in Tkinter
ModuleNotFoundError: No module named 'Tkinter'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "models_GUI.py", line 9, in <module>
from tkinter import * ## notice lowercase 't' in tkinter here
File "~/.pyenv/versions/3.7.5/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I will now describe some of my attempts to solve the problem:
Looking at Tkinter section in Python Wiki I tried installing Tcl and Tk.
%sudo pacman -S tk
but it was already installed. after that I tried installing with pip:
%pip install tk
and
%pip install tkinter
and the error remains the same.
I also tried to create a symlink with this code:
%ln -s /usr/lib/python3.8/lib-dynload/_tkinter.cpython-38-x86_64-linux-gnu.so _tkinter.cpython-38-x86_64-linux-gnu.so
the symlink was created in the following folders:
~/.ve/Coursera_DSP/lib/python3.7/lib-dynload
and
.pyenv/versions/3.7.5/lib/python3.7/lib-dynload
But I still get the same error.
I appreciate it if anyone has any suggestions and I apologize for the language errors since English is not my mother tongue.
After an incessant search on the internet, I believe the problem is related to pyenv and TCL/TK.
I don't understand much about the subject but I suspect that in the creation of the environment by virtualenv python has lost the connection with TCL/TK. Does that make any sense?
Here is step by step guide to make IDLE and tkinter work. Working for me on macOS Catalina. Should be easily adapted to Linux environment:
install tcl-tk with Homebrew. In shell run brew install tcl-tk
in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
reload shell by quitting Terminal app or run source ~/.zshrc
after reloaded check that tck-tk is in $PATH. Run echo $PATH | grep --color=auto tcl-tk. As the result you should see your $PATH contents with tcl-tk highlighted
now we run three commands from Homebrew's output from step #1
in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
in shell run export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall <your python version>. E.g. pyenv uninstall 3.8.2
set environment variable that will be used by python-build. In shell run PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
Note: in future use tck-tk version that actually installed with Homebrew. At the moment of posting 8.6 was the actual
finally install Python with pyenv with pyenv install <version>. E.g. pyenv install 3.8.2
Test
in shell run pyenv global <verion that you've just installed>
now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".
now check tkinter. In shell run python -m tkinter -c "tkinter._test()". You should see test window like on the image:
That's it!
My environment:
check this is something went wrong executing steps above:
macOS Catalina
zsh (included in macOS Catalina) = "shell" above
Homebrew (installed with instructions from Homebrew official website)
pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub)
Python 3.8.x - 3.9.x (installed with pyenv install <version> command)
mac Monterey (M1)
brew install python-tk
python 3.9.10
My basic install of linux mint 20 contains python3.
On windows machines this includes the tkinter package.
My solution was to
sudo apt-get install python-tk
or
sudo apt-get install python3-tk
and live was sunny again.
I created a venv using Python 3.10.5 (alternate version to default 3.8). Tkinter failed to install when I used the command "sudo apt-get install python3-tk"; however, when I used "sudo apt-get install python3.10-tk", the module installed properly. Apparently, using the specific python3 version made the difference.
Since I couldn't find any solution I just uninstalled my whole environment system (pyenv pyenv-virtualenv virtualenv and virtualenvwrapper) and installed conda instead. Now everything works.
Probably I messed up with pyenv installation.
Thank you all :D

Pip on only installs to python27 while i need it to install on python37

I have multiple instances of python in my computer namely python27, python3.6 and python 3.7.
The import module for docx worked daily on python27 until suddenly it stopped working today. I tried installing the module again using pip in windows command line.
It says only installed in python27 directory. But there's an
importError on my script.
I guess I should be high time i transferred into python37 but I can't seem to make pip install into python 3.7
Can someone offer some advice as to the messy situation I'm in?I want to transfer to python3 and install docx in python3
Already checked modules using pip and docx is not there.
You should try setting your command prompt on the version of Python you want the module to install. Then, just use "pip", to install.
This is how you can do this:
Go into File Explorer (hopefully you have windows) and press and hold the shift key, then right click the folder that you want command prompt to look at. In this case, the folder with the version of Python. Then you click on the command prompt selection, and you're golden. Just use "pip install xyz" from there on. Glad to help
- BURAK ILOGLU
for python 3 try below given approaches.
sudo apt-get install python3-pip
this should get you pip3 and you can use pip3 to install packages specific to python3
and also
python3 -m pip install <package name>

Trouble installing using PIP

I'm having trouble importing plotly using pip install. The error I get is as follows:
The following command must be run outside of the IPython shell:
$ pip install plotly
The Python package manager (pip) can only be used from outside of IPython.
Please reissue the `pip` command in a separate terminal or command prompt.
I'm very new to programming, so I simply tried this in the windows command prompt and it said "pip is not recognized as an internal or external command, operable program, or batch file". I'm using Spyder if that helps, but I'm not sure what I'm missing here.
Thanks for your help.
I guess you forgot to put ! before pip.
try:
!pip install plotly
in your notebook, should work.
For windows the cleanest way to install a python package is to:
python -m pip install [packagename]
This removes the ambiguity if pip is added to the path variable or not.
If you are getting below error while installing any python packages in window using command "pip install packageName".
The following command must be run outside of the IPython shell:
$ pip install configobj
The Python package manager (pip) can only be used from outside of IPython.
Please reissue the `pip` command in a separate terminal or command prompt.
See the Python documentation for more information on how to install packages:
https://docs.python.org/3/installing/
solution: add "!" at beginning of pip
!pip install configobj(packageName)
It will work. It worked in my case.
What worked for me was going to the directory in which Spyder is located, which you can do by searching Spyder on your computer and then right clicking โ†’ directory, and then in the directory opening the Anaconda Prompt. In this Prompt you can type in:
pip install [packagename]
and it will work.

How to install pygame windows 10?

I just tried to install pygame for python 3.4 my windows computer, but apparently I need to extract the whl file? I have seen other questions like this one on stackoverflow and it says to write in commant prompt:
pip install package-name.whl
However, when I tried this, I got in return:
'pip' is not recognized as an internal of external command, operable program, or batch file.
Is this because I have Windows 10? Please help!
For me this command works.->py -m pip install -U pygame --user
But first, make sure you properly configure your environment variables.
This tutorial http://www.aaronstannard.com/how-to-setup-a-proper-python-environment-on-windows/ will help you how to setup environment variables for python, make sure you do it correctly only step 2 ,no need to do any other steps from this tutorial, now you can follow my steps that were listed below. I will assume that you have already installed python in your computer
Step1
Search for cmd and Right click on cmd and do run as administrator.
Step2
Run the command, py -m pip install -U pygame --user, but before you run this command first of all make sure you configure your environment variables properly.
Step3
Type python in command prompt, this will take you to the python command line which will look like this ">>>".If not then you have a problem with your system variables.
Step4
Make sure you are in python command line and then type import pygame,and after this press ctrl+z and enter that will take you out from python command line.
Step5
Make sure you are not in python command line and then run this command py -m pygame.examples.aliens,if the game starts then congrats you are done.
It can be possible that the pip/ wheel is not installed
follow this link to install pip link
If already installed maked sure it is updated if not
You can use this code to update: python โ€“m ensurepip โ€“-upgrade or pip install --upgrade pip
To install wheel pip install wheel or upgrade pip install wheel --upgrade
once this is done you are good to install pygame
At the command line, make sure youโ€™re in the same directory as the .whl file and run
pip install package-name.whl
The issue is your pip isn't working correctly. To make sure it works, go to this folder C:\Python34\Scripts in windows explorer, hold shift and then right click. press 'open command prompt' here and then try to run pip.
That error occurs when the command entered, pip in this case, isn't found.
I just got pygame working on Windows 10 with these steps.
Download pygame-1.9.2a0-cp35-none-win32.whl from UCI.
Run the cmd shell and enter
> cd C:\Users\<me>\AppData\Local\Programs\Python\Python35-32\Scripts
> pip install C:\Users\<me>\Downloads\pygame-1.9.2a0-cp35-none-win32.whl
Start the Python 3.5 console. Therein enter
import pygame
dir(pygame)
No errors from either means that Pygame is ready for your command.
pip install pygame
that's all you need, just remember to use it correctly in your code
assuming that you have pip installed. You can run:
pip install pygame
in command prompt or a simulated terminal.
If you don't have pip already installed, please refer to this: (hope I helped!)
https://www.makeuseof.com/tag/install-pip-for-python/
I updated my pip, then ran the command prompt (cmd) as adminstrator, then I issued a python command:
-m pip install pygame
And it installed pygame.

Resources