python2 and python3 on Ubuntu - python-3.x

I recently installed Ubuntu 18.04.4 which came with Python 3.7.6 installed. I installed Atom v1.45, which comes with Python 2.7 automatically.
When running a Python script from the terminal, it raises ModuleNotFoundError. I figured that if I ran the script from the terminal with python3.7 file.py instead of python file.py there is no problem, though it is slightly annoying to do that all the time.
I uninstalled python2.7 but then I could not use Atom.
How can solve this problem?

I faced a very similar problem. Some of the tips from my end are:
I recommend using a virtual environment when running scripts from the command-line. This helps resolve all module dependencies for the script in general and especially when dealing with multiple Python versions.
$ virtualenv -p /usr/bin/python3 ./venv
$ source ./venv/bin/activate
$ ./venv/bin/python foo.py
Typically, the above steps, are either in my Makefile or in a helper script.
When running a script from the terminal, if you want to type python instead of python3.7, you can create a symlink for python to point to python3 in the /usr/bin directory.
Also, I recommend switching to python3 completely since support for python2.7 has officially stopped.

Related

Run scrapyd in Python 3.6

I've been looking around and I can't seem to find an answer on how to run scrapyd in Python 3 and above. When I run it it keeps defaulting to python 2.7, though I recall reading in the docs or elsewhere that scrapyd supports python3:
https://scrapyd.readthedocs.io/en/stable/news.html#id1
Running on an AWS Ubuntu 18.04 server. What am I doing wrong? How can I change to Python 3.4 and above?
I'm having problems because I'm using abstract inheritance in my spiders which python 2.7 doesn't support.
Edit: I'm able to run Scrapyd in Python 3 using a virtual environment, but how would you do it without one?
Simplest solution is use a virtual enviroment.
Since you are using Ubuntu and you can make it work using a venv I assume you installed scrapy using pip instead of pip3. Since Py2.7 is the stardard reference to python in Ubuntu, when running pip it will execute pip for py2 instead of py3.
Another way would be uninstalling pip uninstall scrapy and installing it again using pip3 pip3 install scrapy. This should fix the reference to scrapy. If by any chance you also use scrapy in py2.7 and therefore can't uninstall, then you have another reason to use venv.

virtualenv couldn't find python interpreter path?

my computer have python3.4, python3.5, python3.6, python3.7, but now i want to use python virtualenv to create a env , but it show path not found?
The image in here:
You need to provide the full path to Python, for example:
virtualenv.exe -p C:\Python37\python.exe
For git-bash the command should be slightly different:
virtualenv.exe -p /c/Python37/python.exe
First of all having multiple versions of Python is not advised especially if you cant manage them. The issue you are getting stems from the installation of the Virtualenv package. You need to use the python version that has Virtualenv in the site-packages. Examine all the site packages for the 3 versions then use the one that has virtualenv alternatively you can uninstall all the 3 versions and reinstall one I'd advise on Python 3.6 then install virtualenv via pip and try again.

Cannot install python3.6 on Debian 4.9 VM

I'm running a Debian VM (4.9.189-3+deb9u1) on the Google cloud. I want to run a script on this instance; the script works fine on my local machine (Mac OSX Mojave 10.14.6; python 3.6.8). However, when I run it on the VM I get an error that seems to relate to the fact that my the VM runs python 3.4 when the script needs python 3.6+.
Here's the problem. When I run python -V, the response is Python 3.7.4. However, when I try to install the library supporting the script I want to run
pip3 install --user --upgrade -e
git+https://github.com/twintproject/twint.git#origin/master#egg=twint
I get
twint requires Python '>=3.6.0' but the running Python is 3.5.3.
I have tried changing the default python as detailed here; this doesn't seem to work––in fact, python 3.6 isn't even visible as an option when I query ls /usr/bin/python*. Can anyone offer some advice on how to proceed here? Thanks.
It seems like you have multiple Python versions available. If python -V gives you Python 3.7.4, you can use:
$ python -m pip install --user --upgrade -e git+https://github.com/twintproject/twint.git#origin/master#egg=twint
to invoke pip from that same Python interpreter.

Why virtualenv shows all packages installed and do not install modules in virtualenv?

Question
I am not sure why when virtualenv active, pip freeze stills showing all modules when it suppose NOT to. What am I doing wrong?
On the terminal
I tried two ways to create a virtual env:
virtualenv my-virtualenv
virtualenv --no-site-packages my-virtualenv
then activate it
source my-virtualenv/bin/activate
Results
Both show all packages (when they suppose not to). Running command pip freeze I get:
(my-virtualenv)$ pip freeze
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
actionlib==1.11.9
angles==1.9.11
bondpy==1.7.19
camera-calibration==1.12.20
camera-calibration-parsers==1.11.12
catkin==0.7.6
cv-bridge==1.12.4
diagnostic-analysis==1.9.2
diagnostic-common-diagnostics==1.9.2
diagnostic-updater==1.9.2
dynamic-reconfigure==1.5.48
gazebo-plugins==2.5.13
gazebo-ros==2.5.13
...
rosnode==1.12.7
rosparam==1.12.7
rospy==1.12.7
rosservice==1.12.7
rostest==1.12.7
rostopic==1.12.7
rosunit==1.13.5
roswtf==1.12.7
tf2-ros==0.5.16
topic-tools==1.12.7
xacro==1.11.2
Running
(my-virtualenv)$ which python
/home/user/.../my-virtualenv/bin/python
(my-virtualenv)$ python -V
Python 2.7.12
and
(my-virtualenv)$ which python3
/usr/bin/python3
(my-virtualenv)$ python3 -V
Python 3.5.2
Hence, I even can use python3 when virtualenv is activated but it uses the pc installed module. Moreover, new installed packages are install in the pc and not the virtualenv and I can't install python3 in the vitualenv because it "exists" already (but in the pc).
I was having the same issue and it was because, somehow, Python configuration of ROS was generating it.
I solved it removing the source commands of ROS from the ~/.bashrc. E.g.:
source /opt/ros/melodic/setup.bash
source $HOME/ROS/aslam_ws/devel/setup.bash
By the way, I realised that, before changing anything, creating the environment from PyCharm works properly too (when in the terminal I still had the issue).
There must be a better solution but I haven't found it yet.

"Error: Tried to guess R's HOME but no R command in the PATH"

"Error: Tried to guess R's HOME but no R command in the PATH"
after runing [ rpy2-2.5.6]$ python setup.py install commands in my redhat server
Try to install libraries always with pip instead of messing around yourself with setup.py files.
I run into the same error under windows 8, R Version 3.3.1 and Python 2.7, 32bit. The solution is easy but for this case the normal pip install rpy2 does not work.
Instead try to use the Windows-Binaries you find them here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2. Download the suiting whl file for your configuration. Then go to your download folder in cmd and simply use:
pip install rpy2-2.7.8-cp27-none-win32.whl
Note, make sure you adapt this code to your download file.
I found out that this happens to me because I'm trying to install on a python version 2.6.5 and the setup.py script tries to use a method subprocess.check_output that is new on 2.7, hence it raises an exception that leads to the this message. It seems to be wise to use a 2.7 or later version, because the check_output method is used several times in the setup.py script.
This worked for me on my Mac using python 2.7:
In my ~/.profile (or use ~/.bashrc)
I set:
export R_HOME=/Library/Frameworks/R.framework/Versions/Current/Resources/
export PATH=$R_HOME:$PATH
sourced it (source ~/.profile) and then did the standard
pip install rpy2

Resources