Python 3 virtual environment is executing python 2 - python-3.x

This issue has had me scratching my head for a while now.
I've encountered this problem twice now, on windows 7 and windows 10. As its a closed network I've no security concerns with W7.
I have an existing Python 2.7 install - densely populated, with some fairly old library versions.
Whilst I would like to create a virtual environment for Python 2.7 I'm reluctant to do so because wheels are not available for many of the library versions I need to use, and revision histories do not appear to be available for some of these older libraries either, so without trying them I can't tell if the later library versions are backwards compatible or not. As the Python 2.7 code is from a production environment I can't break/replace it.
The python 2.7 libraries are all installed from executable installers as many of them predate
the introduction of the python wheel standard. Some also require to be built with older versions of MSVC, which is another reason for not wanting to attempt to rebuild.
It will be much easier to leave the python 2.7 install as is and start running Python 3 in virtual environments, although my original plan was to create a separate 2.7 virtual environment.
Example Python 3 virtual environment created as "py37".
In my virtual environment.
include-system-site-packages = false
When I activate the virtual environment all appears to be good.
Python -V returns 3.7.3, which is what I expect
I modified my script to output the python version (so I can track what is happening)
(py37) C:\my_folder\my_script.py
3.7.3 (v3.7.3:ef4c6ed12, Mar 25, 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]
Traceback (most recent call last):
file "my_script.py", line 24, in (module)
from tk import ttk
ModuleNotFoundError: No module named 'tk'. 'tk' (python 3 lib) is not being found.
I then created two simple scripts, one using the python 2 print format, the other python 3.
Even though the virtual environment says python 3, and the python version says 3.7.3 the python 3 script fails to run.
The python 2 version of print prints out correctly, so clearly even though all indications are that Python 3 is running, it's actually Python 2.
PythonPath is null/not defined on the PC and my path variable points to my python 3 virtual environment, not my Python 2 install.
All the searching and examples I have come up with stop when version() or Python -V returns 3.x, so if it is a duplicate issue I've not managed to find the original request.

The solution to this was simple, but unpleasant.
In order for everything to work as it should I needed to explicitly identify the executable I wanted to run, every time I wanted to run it i.e. full path to the python executable in my python3 virtual environment.
I ended up creating a batch file wrapper to facilitate this
e.g. c:\apps\my_venv3\scripts\python.exe %*
Clunky, but it works, reliably, and does not break any of the existing environments.

Related

Pandas install or import in IBM SPSS Statistics Version 26

I have installed the latest version of IBM SPSS Statistics(Version 26) which has pre installed python 3.4 and 2.7. i am trying to use version 3.4 python. i am able to import modules like pip , sys , os etc. i tried pandas the same way. i am unable to do so. getting error no module found. Hence going through our forum and IBM support did the following changes.
received the following error
1) tried pointing the site-packages via
import sys
# Assuming windows and standard python folder here.
sys.path.append(r"D:\Python34\Lib\site-packages")
2) changed the path in the settings of SPSS
3) Tried installing pip in the below folder as suggested in the forum but got message i have already installed the updated version.
C:\Program Files\IBM\SPSS\Statistics\Subscription\Python3
4) following versions of python were installed
have tried what i could. Need your expertise help to fix the same which will help me to install/use modules needed for SPSS. Thanks.
This is going to be painful to explain, I'll do my best.
As far as I can tell, you're on windows. Usually when we need a new package, we just open cmd and type pip install xxx (assume you added python to path when installing it). The reason that this works, is because when you type pip install xxx in cmd, windows recognize pip to be a command because python path is in system variables. Windows know that I can execute pip install with this python path.
However for SPSS python (3.4), that python had a different path in the system. Thus when you only have the 3.7 or 3.8 python in path, windows cannot install package to you 3.4 python, and I'm not sure if you can have more than one python path in system.
In order to fix this, you need to first figure out what's the path to your 3.4 python, then in this page you can follow the instruction to remove your 3.7 or 3.8 python in path, and add your 3.4 path, then you can do pip install xxxx for whatever package you want
I did the same thing with a arcgis python distribution, hope this works for you. If the attached page does not work, just google add python path to windows and look for a instruction that works on you PC
Oh and the reason that you can import pip, sys and some other package but not pandas, is because python is 'battery included', it comes with tons of packages pre-installed for additional functionality, but pandas is not one of them.
Fixed it since my ananconda had version 3.7 .i created virtual environment and installed 2.7 python with anaconda package. Pointed SPSS to the 2.7 folder and was able to import pandas.

Python 3.6 not finding modules that Python 3.5 can find

I am trying to make a discord bot based on this repository https://github.com/sleibrock/discord-bots
It uses Python 3.6, but when I try to use it I get the error
Traceback (most recent call last):
File "Bot.py", line 9, in <module>
from discord.py import Client, Game
ModuleNotFoundError: No module named 'discord'
In python3.5 >>>import discord runs fine.
In python3.6 >>>import discord gives the same error as above.
Any help would be appreciated.
edit: I copied all files from /usr/lib/python3.5/site-packages and /usr/lib64/python3.5/site-packages to the python3.6 counterparts which seems to have fixed the problem. It seems like it wasn't installing anything into the python3.6 folders and was checking the 3.5 folders, making something break with the import.
Python allows for different versions being installed independently of each other. Each one will have its own packages, which make sense because some packages require a minimal (or specific) version, and packages for Python2 could not work in Python3. In addition, packages using compiled C library will be different between 32bits and 64 bits versions.
It is a feature, because it allows you to install some packages only for one of the different versions you have on your system, but it also mean that you have to install them in all the versions you want to use with them.

can't import http in python 3.5.1 but it works in cmd

I'm still learning python. I have windows 8 and had downloaded 2.7 as well as 3.5 because two modules I used in the past respectively used different python versions. Now though, I'm trying to run a script where the first line is import http.client or http. Neither of these work though. In cmd, python returns 3.5.1 currently and import http.client and import http return with no errors but in my IDE these don't work. Why not?
You can follow the Pycharm documentation here to change the Python version for your project from Python 2 to Python 3.
(In particular, Selecting Python interpreter for a project section)

How to install PyGObject on windows in a anaconda virtual env

I want to use Gtk with python under windows. I already have Anaconda installed on windows. In order not to mess up everything and to have some easiness uninstalling/reinstalling, I would like to have a virtual env created with conda, working with that Gtk installation. But I don't seems to be able to make it work.
Here is my process. I first create a raw Ananconda virtual env with
conda create -n gtk-exporter python
The virtual environment is located at C:\Anaconda3\envs\gtk-exporter.
I then download the latest windows installer for PyGObject at http://sourceforge.net/projects/pygobjectwin32/files/?source=navbar
I extract and execute the installer and tell it to use a portable python install at C:\Anaconda3\envs\gtk-exporter. I only select Base, GTK and Glade for installation. The installation finished in a second and says it's successful.
Then within windows' shell, I activate the new environment with activate gtk-exporter. However when I try to import gtk, it fails, not finding gi.repository.
>>> from gi.repository import Gtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
What's wrong here ? C:\Anaconda3\envs\gtk-exporter\Lib\site-package contains a folder gnome with a lot of stuff including *.dlls, *.exe's and unix-looking folders like etc, lib or share, but I don't see a init.py or something pythonic. Am I missing a step.
Thank you for your help !
The problem was that I used python 3.5, whereas it is not supported. The installer should not have allowed me to install with python 3.5. I filed a bug report to signal it.
I solved the problem by uninstalling python 3.5 and installing python 3.4.

How can I make my script choose the right python interperter?

I have both 2.7 and 3.0 versions of the Python interpreter installed (on my Ubuntu 32 system), but one particular script uses 3.0.
Using
#!/usr/bin/python3 -B
will not work when the program is run with python myprogram.py.
And I also need a solution that works also in Windows where I also have both python versions installed.
How can I make the script to run only with the right python version?
Please use virtualenv, which makes isolated Python environments easy.
python = Python to use. # This has to be the absolute path to Python executable
os.execl(python, python, * sys.argv)
This way you can restart the script with the python you want to use. Not really stylish.
I don't know why you can't just launch the program with python3 foo.py, but it's possible to have a python2 program relaunch itself as python3 with something like this.
import sys
if sys.version_info.major != 3:
import os
# replace this process with a python3 process
os.execlp("python3", "python3", *sys.argv)
It's a bad solution though, because now your python3 program can't use anything that's not valid python2 syntax
Please take a look at The wrong python interpreter is called
You have to choose a correct interpreter based on where you installed the desired version of Python and your system variables.

Resources