Python 2 does not work any more, PYTHONPATH issue? - python

I use python3 normally but also some python2 with ROS. After a longer time i tried to built my ros workspace and the error I get is
ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg'
Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.
CMake Error at /opt/ros/kinetic/share/catkin/cmake/safe_execute_process.cmake:11 (message):
execute_process(/home/work/py_venv/bin/python
"/opt/ros/kinetic/share/catkin/cmake/parse_package_xml.py"
"/opt/ros/kinetic/share/catkin/cmake/../package.xml"
"/home/work/catkin_ws/build/catkin_tools_prebuild/catkin/catkin_generated/version/package.cmake")
The first directory /home/work/py_venv/bin/python is a virtual environment for python3. I am not sure why it is looking there. I dont have any virtual environment activate (well on purpose at least). What I am totally missing though is the path to the standard python2 installation. Shouldnt it be there?
If I enter echo $PYTHONPATH in the terminal, I just get /opt/ros/kinetic/lib/python2.7/dist-packages
I guess there is something missing? How can I set this variable correctly. Which value does it have at installation?
I recently installed venv in python3 apart from that I am not aware of any changes I did to python since it stoped working.
Now that is a lot of questions.
If you can answer any of them, it would be appreciated.
Edit: I know found the main problem is the prepending of the venv on the path , so that it look for the package in the python3 installation which does not contain the catkin package. How can I remove this from the path?

If you want to remove venv python path, you can do that as follow,
echo $PATH
export PATH=<remove unwanted path>
In this way, you can remove your venv python path.
Also, check this python --version which might be set to python3, if so change that as well.
Finally, check PYTHONPATH if it's not directed proper location change and try.

Related

MinGW still pointing to old python version, even though the path has been removed

I uninstalled my python 3.10.9 and then I installed 3.11.2, the latest version, and yes I did add it to path when I installed it, did everything like before.
After restarting, I checked to see if all is good but python --version was not working so I found out I had to disable python from app execution alias or something like that.
Now python --version works, great, except it does not, it still shows 3.10.9.
I ran where python and it showed me two paths:
C:\msys64\mingw64\bin
C:\Users\USER\AppData\Local\Programs\Python\Python311
So I checked my environment variables and I saw the python311 path scripts being at the TOP followed by the basic python311 path, as far as I know that is how it should be right?
The mingw bin path was way below it, so why it points to that .exe instead of the new one?
I even removed the mingw bin path from the environment variables and restarted the system and absolutely no change...

fixing the path so python can recognize z3 modules

I have successfully installed z3 on a remote server where I am not root. when I try to run my python code I get :
ModuleNotFoundError: No module named 'z3'
I understand that I have to add it to PYTHONPATH in order to work and so I went ahead and done that like this:
export PYTHONPATH=$HOME/usr/lib/python-2.7/site-packages:$PYTHONPATH
I still get the same issue though, how can I verify that it was correctly added to the variables environment? what am i doing wrong?
Did you pass the --python flag when you called scripts/mk_make.py?
See the instructions on https://github.com/Z3Prover/z3/blob/master/README.md on how to exactly enable Python (about all the way down in that page). Here's an example invocation:
python scripts/mk_make.py --prefix=/home/leo --python --pypkgdir=/home/leo/lib/python-2.7/site-packages
Change the directories appropriately, of course.
For Windows users that just downloaded and unzipped the compiled Z3 binary into some arbitrary directory, adding the location of the python directory in the directory where Z3 was installed to PYTHONPATH did the trick. ie in Cygwin : $ export PYTHONPATH=<location of z3>/bin/python:$PYTHONPATH (or the equivalent in a Windows command shell)

Why is my virtualenv's pip listing packages in my lib/python2.7 directory?

In my home, I have a directory named lib/python2.7 (there are actually five directories like that, for different python versions). Since this is a shared hosting (Webfaction), that directory is fundamental to me. There, I have stuff like virtualenv and virtualenvwrapper installed, since as customer of a shared hosting, I have no access to sudo and installing global packages.
However, when I create a virtualenv:
$ mkvirtualenv myenvironment
$ workon myenvironment
$ which pip
# outputs the myenvironment's path to pip
$ pip freeze
The command shows the whole packages list under my lib/python2.7 (this includes the same virtualenv packages, and conflicting packages I have due to... legacy... reasons). This also annoys me if I want to install a package which is the name of a package in lib/python2.7 since it does not allow me to update it.
Right inside the workon environment, I try to check whether the PYTHONPATH has weird stuff, but it is empty:
$ echo $PYTHONPATH
# shows a blank line
It is also empty if I try that command out of any virtual environment.
It seems that --no-site-packages is default but solves just part of the problem. This means: pip freeze | wc -l displays a lesser value when in an environment than when executing globally, out of any environment, which tells me that there are certain already-provided packages that are being excluded and are from the hosting itself (and not installed by me since, again, the hosting is shared and I don't have access to global space).
My question is: How can I solve this? I want my virtualenv not list the packages in $HOME/lib/python2.7
Please avoid dupe-linking to this question, nothing was useful there and still does not have an accepted answer. I wrote this question after reading and trying each solution in that question
I think you need to specify python version. You can specify python version with which you want to create virtual environment using command like
virtualenv -p /usr/bin/python3.4 virt/virtname --no-site-packages
Because when you not specify a python version, virtualenv creates a environment with pythonv2.7 and hence all packages end up in the folder you mentioned.
Found the solution after deeply digging. This is a Webfaction custom but this could apply to any installation like this if the problem occurs.
The core of the problem is that Webfaction configured a sitecustomize.py file for us. The file is located at /usr/local/lib/pythonX.Y/sitecustomize.py and adds by itself the contents of ~/lib/pythonX.Y (and conditionally the contents of any python app under ~/webapps if you are working under a directory of it to run a python script).
Even when the virtualenv's python executable is a different one, it will load the said sitecustomize.py file each time it runs as the base python executable does.
The workaround here? Create an empty sitecustomize.py in your virtualenv to override the other:
touch ~/.virtualenvs/yourvenv/lib/pythonX.Y/sitecustomize.py
And it will work. Take this as reference if you are stuck here like I was
Notes: Replace X.Y on each case with the corresponding version you are working. Additionally remember: You cannot remove or edit the base sitecustomize.py since you are in a shared hosting, in this case. However, overriding will work for each case as long as you do this for every virtualenv you want.

Git bash uses wrong python, other solutions don't work

I've seen a couple solution so far but they don't work. I'm trying to run (in git bash, on windows)
bash scripts/test.sh
But it's supposed to use Python27, and I have that, and Python34. when I type python --version, it tells me it's using Python34. I tried doing
PATH=$PATH:/c/Python27/
But it still tells me I'm using Python34. I know it's in my environment variables, so what am I missing?
$PATH:/c/Python27/ adds /c/Python27/ to the end of your PATH. The directory containing the Python34 executable must be already in your $PATH. So you need to add /c/Python27/ before the other directories listed in $PATH, so it is checked before the directory containing Python34.
PATH=/c/Python27/:$PATH

Install a different version of Python

this might be a very simple question but I need your help. I work in a network and I cannot install the programs I want. Anyway, I need to use another version of python, which is installed in the directory /new_version/.
Now, when I type "python" in a shell (I use bash) the command point to the version of python installed in the machine I'm working with. I'd love that when I type "python" this command point to the /new_version/ which I've installed. It would be also better if I can call this "new version" with another command, i.e. python2.
I tried changing the PYTHONPATH in the .bashrc but it didn't work.
alias newpython="/path/to/your/new_version/python"
Add this to your .bashrc, you can then start the new python with newpython and the standard one with python.
Add the line
export PATH=/new_version/:$PATH
to your ~/.bashrc (or ~/.bash_profile) file. Then, whenever you run python, it will find the new version first in your PATH. Note this is PATH, not PYTHONPATH. See the comment by #Aaron.
Edit: Only do it this way if you want python to point to the new version. Use an alias as #cularis suggested if you want to call it something different, or make a symlink with:
ln -s /new_version/python /path/to/a/dir/you/add/to/your/path/newpython
Install virtualenv. With this you can easily set up different Python versions like that:
virtualenv -p /new_version/bin/python
Also, virtualenv enables you to easily install other Python packages via pip install.
And finally, there's a package called tox which can automate testing with different Python versions...

Categories

Resources