Python console_script entry point runs as Python2 - python

I have a package that sets up an entry point console_script and i want the console_script to run the python function/script as Python3
My package uses the urllib.request module (Python3).
Running the command (the console_script) gives a python error about request not being a module, thats why i'm thinking it executes my script as Python2..
__main__.py
import urllib.request
...
...
urllib.request.urlretrieve(url, file_path, progress)
...
error
ImportError: No module named request
python --version
Python 2.7.13
python3 --version
Python 3.5.4rc1

console_script scripts are executed using whatever version of Python was used to install their package, e.g., if you installed the package with pip, pip2, python2 -m pip, or the like, the script will always run under Python 2. Either uninstall the package and re-install it with Python 3 or else use something like six to ensure your script will work under both versions.

Fixed it by uninstalling my package from all pip's (pip, pip2, pip3)
and then reinstalling my package using sudo pip3 (notice the sudo part ..)

Related

How to resolve Python import module?

I'm on Ubuntu 20.4. When I execute a Python script, I get the following error and sys.path.
I installed the packages with pip3 and they are located in
/usr/local/lib/python3.8/dist-packages
When I look at the installed packages for python3 and python3.8 interpreter, I see "binance" package installed and recognized.
I tried to set PYTHONPATH to point to my project folder, but that didn't help either.
echo $PYTHONPATH --> /algos_python
You have installed binance from PYPI but you are not using it. You are using python-binance and you need to install it. (Using python3 -m pip install python-binance) and then run your code again. See this for more info on this.
Try these:
python3 -m pip install python-binance
If you are calling your file binance.py (Or have a file/folder named that in your directory), and then trying to import from binance.client, python will look in your binance.py file instead of the library. So try renaming your local file.

Why are python modules not found despite installing them in pip?

I've been trying to use VS Code's python debugger on Linux (mint), which uses debugpy and it keeps giving the error "No module named '_ctypes'". Installing libffi-dev didn't fix it as suggested elsewhere on SO and neither did reinstalling python and python3; so, I tried installing debugpy through pip:
pip install debugpy
Which installs with no issues. However, both python and python3 commands cannot find the module despite the fact that the module is installed (which I can see when I enter pip list)
python -m debugpy
/usr/bin/python: No module named debugpy
python3 -m debugpy
/usr/local/bin/python3: No module named debugpy
So after trying to reinstall pip multiple times, I tried installing through the pip module
python -m pip install debugpy
/usr/bin/python: No module named pip
python3 -m pip install debugpy
/usr/local/bin/python3: No module named pip
So it seems my pip module is also missing too. It may have something to do with my multiple installations of python3 as it seems that there is one in /bin and in /usr/local/bin and the local installation is the one that gets called with the python3 command according to which python3.
This leads to multiple questions:
Should the pip module be installed in python, and if so how do I install it again?
How can I get the pip command to actually install the modules into python?
How can I ensure that there is only one python3 installation in Linux (mint/ubuntu)?
Thank-you. If it helps answer the question, I do not seem to have a PYTHONPATH variable.
i had same issue but it can be fixed by two methods.
reinstall pip for the latest python version you use.
2.use the version default python version which came with your python install
On ubuntu you can try to do sudo apt install python-pip python3-pip. Also there a couple ways you can try what PIP's documentation recommends: Installation
You can try checking sudo update-alternatives --config python maybe you'll see several python installations there.
Also you can check pip contents to find out what python binary it uses:
$ which pip
/usr/local/bin/pip
$ cat /usr/local/bin/pip
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
#!/usr/bin/python3
I tried installing it with pip and it has worked for me. The problem you might have is that it is not installed in the correct version of python (e.g it installs for python2 instead of python3 or it installs in python3.8 instead of python3.9).
What you can do to avoid this problem is to create a virtual environment, as is explained in this link: https://docs.python.org/3/tutorial/venv.html. If you do that, remember to change the interpreter path in VSCode. At the bottom left of the screen there should be the python version that you are using. If you click on it you see different interpreter versions that you can use. Select 'Enter interpreter path' and manually choose the directory where you have saved the new python virtual environment.
It's difficult to narrow down a solution for apparently you have done a mess... To start, do yourself a favor:
Don't touch your System's Python!
Again, it is difficult for me to understand what is the current state of your Python scene... just promise me you'll fix your System's Python 2/3 (i.e, guarantee pip 2/3 there, using your system's package manager (apt, yum, etc)).
Then, start using some virtual environment manager, nowadays we have Pipenv (https://pipenv.pypa.io/), which can be a bit cumbersome at first but -- trust me -- in ~1 hour you'll love it.
Conda (https://docs.conda.io/) is also a great env manager (or the classics (pyenv, venv, etc...)).
...Just pick one and leave your OS' Python alone. You'll see that your issues will not only get simpler to diagnose but you'll also be able to sleep in peace ;)

How do I run a Python script with sudo? (MAC)

I want to test the keyboard.on_press_key() function, but OSX blocks it naturally. When I try to run it through the terminal using sudo python [pathtofile] I get an import error: "ImportError: No module named keyboard". I tried installing the module using sudo (sudo pip install keyboard) but, even though it installs just fine, I still get the same error. 
What am I doing wrong?
Note that python on macOS refers to the system installation. You should not install packages via pip to the system installation of Python. I'd recommend you to install another Python interpreter e.g. via brew. See www.brew.sh for more info.
Then, it is just a matter of installing Python via
$ brew install python3
After that, simply install your keyboard package again:
$ pip3 install keyboard
That is a error in your script, you probably didnt import the keyboard module before you used. Try adding this in the first lines of your python script:
import keyboard

"No module named pefile" even though I have installed pefile

I have Python 2.7 and Python 3.8 on the same computer
I am trying to get this to work: https://github.com/countercept/python-exe-unpacker
The requirements are:
pefile==2017.9.3
unpy2exe==0.3
uncompyle6==2.11.5
xdis==3.5.5
pycrypto==2.6.1
configparser==3.5.0
And I guess the other stuff is installed correctly (not sure). But when I try to currently run this thing with python python_exe_unpack.py -i [programname.exe]
I get the error:
C:\Python27\python.exe: No module named pefile
I've run both of these commands:
py -m pip install pefile==2017.9.3
py -m pip install pefile==2019.4.18
and the problem persists.
Any idea what might be going wrong?
You should use pip freeze to obtain the list of installed packages. Probably your py and python executables aren't the same, try py python_exe_unpack.py -i [programname.exe]. Also check which paths you using to obtain that package:
import sys
print(sys.path)
You can add extra paths through PYTHONPATH environment variable or just sys.path.append("/path/to/folder") with path where is you're installed pefile.

Python 3.8 on Windows 10/Linux (Manjaro 18.1.5): ModuleNotFoundError for TestPyPI package installed with pip

I followed along with the package distribution tutorial for uploading a package on test.pypi.org from a Linux machine, and proceeded to try to install the package on a separate Windows machine.
On the Windows machine, I installed Python 3.8 and ticked the Add Python 3.8 to PATH box. No previous Python installation was present, and it installed to C:\Users\me\AppData\Local\Programs\Python\Python38\.
In Environmental Variables, my PATH under "Users variables" is: C:\Users\me\AppData\Local\Programs\Python38\Scripts\;C:\Users\me\AppData\Local\Programs\Python38\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
My PATH under "System variables" is much longer, but it contains no Python directories.
I then downloaded and installed pip.
In Command Prompt, I first created a virtual environment with py -m venv tutorial and activated it with Scripts\activate. Activation was verified by the (tutorial) next to the prompt. Then, in the virtual env, with py -m pip install ... I installed my example package.
I can verify it's installed by seeing it in the output of py -m pip list:
Package Version
----------------------- -------
example-pkg-me 0.0.1
pip 19.2.3
setuptools 41.2.0
example-pkg-me does not appear outside the virtual env.
The tutorial finishes by having you attempt an import in the Python interpreter:
py
>>> import example_pkg
but I get ModuleNotFoundError: No module named `example_pkg`. Why? Is something wrong with either of my PATHs? Note that the tutorial specifically mentions omitting the username (and import example_pkg_me issues the same error regardless).
Curiously, when trying to install and import a "mainstream (?)" package, it succeeds with no errors. I.e.,
py -m pip install numpy
and
py
>>> import numpy
works.
Update:
Same results on a separate Linux machine with the same steps where the only differences are that Python and pip were already present.
As an aside, if your package is intended to be run (as a GUI app) rather than imported in the interpreter as in the tutorial, how would the person who installed it run it? Would they have to navigate to the directory where the package files were physically installed and run a main.py script there? Is there a better way (like freezing the package as an executable with the likes of cx_Freeze and distributing the EXE instead of relying on pip installations)? Or maybe just expect them to download a repository, run py setup.py install, and then a main.py script?

Categories

Resources