Python interpreter, virtual environments and pycharm - python

Platform Windows. IDE PyCharm CE.
I have a script that uses module ArcPy from ESRI. This module has its own Python 2.6. I have to import a module that uses dictionary comprehensions not supported by Python 2.6.
How do I work around this without rewriting the code to avoiding list comprehensions?
Other questions:
What should be the correct pattern for project creation and interpreter maintenance? Should I always use virtual dedicated envs for each project importing any needed modules and keep isolation for each?
Is it correct to import the extra needed packages for my projects to the ArcPy python installation Python 2.6.5 (C:/Python26/ArcGIS10.0/python.exe)? Can this cause problems later to arc map?

You can upgrade your ArcGis Python 2.6 release to 2.7.
If you need Python 3, it won't work, because arcpy works only in Python 2.

Related

Multiple Python Version Env Setup

I'm working in the VFX industry and we deal with different software packages that ship with their own Python interpreter. We are running on Linux and use modules to handle our environments to make sure that people are using the correct version of all applications depending on the project they are working on.
Since months, we are trying to setup an environment that supports multiple versions of Python. And what is blocking right now are additional Python libraries that we are using in our in-house tools, like sqlalchemy, psycopg2, openpyxl, zmq, etc.
So far, for each project, we have config file that defines the version of each package to be use including the additional Python modules. And to use the correct Python version of each Python module, we look up the main Python interpreter defined in that same modules definition file. This works as long as the major and minor versions of all Python interpreters do line up.
But now, I would like to start an application that ships with a Python 3.7 interpreter and another application with a Python 3.9 interpreter and so on. All applications do you use our in-house tools which need the additional Python modules. Of course, this fails when trying to import any additional module.
For now, the only solution that I see is to install the corresponding Python modules in the 'site-packages' of each application that comes with its own Python interpreter. That should work. But this means that we have to install for each application version all necessary Python modules (ideally, the same version of it to avoid compatibility issues) and when we decide to update one of them, this needs to be done again for all 3rd party applications.
That does not sound super-efficient to me.
Do you have similar experiences and what did you came up with to handle this? I know, that there are more advanced packages like rez to handle complex environment setups, but although I do not know the details of rez, I could imagine that the problems stays the same. I guess that it is not possible to globally populate PYTHONPATH with additional modules so that it works on multiple Python interpreter versions.
Another solution that I could imagine is to make sure that on startup of each application that needs additional Python modules, we do our own sys.path modification depending on the interpreter version. That would imply some coding but we could keep a global version handling without installing them everywhere.
Anyway, if you have any further hints, please let me know.
Greets,
Carlo

Module Not Found Error with one version of of Python, but no the other

I am using a VS Code (Linux Mint) plug-in for Python that allows me to choose between different interpreters.
When I run my file with Python 3.8, I get an error saying that it can't find pandas I am trying to use, but when I switch the interpreter to Python 3.6 there is no issue finding pandas.
What's going on?
The module might not be updated for Python 3.8. Search up the module's name from GitHub or where it can be imported from to see what versions they support.
That module isn't installed for Python 3.8, simple as that. Different versions of Python have different libraries (listed under sys.path) on Debian-based distros.
Without knowing which module, it's hard to say much else, like whether it even supports 3.8, but to install it, you might be able to use apt or pip.

Install python3 beside Enthought Canopy

I'm developing python code that needs to run under 2.6+ and 3+ (I'm using python-modernize to make the code compatible). The code under development has no C extensions or 3rd party Python package dependencies except for pexpect3.3 which is used solely for testing.
How can I install python 3.4 on OS X 10.9.5 for the sole purpose of testing the code under development, i.e. I want to be able to run 'python3 myscript.py' but otherwise be sure that python3 will not be invoked as 'python' and without compromising the Enthought Canopy python2.7 environment that's my day-to-day standard for all other python work?
I'm aware, from other SO answers, e.g. Python 3 in Enthought Canopy, that Canopy doesn't support 3.x, so I'm not trying to make it work in that environment. I just need 3.x for some command line testing.
Should be no problem at all. Just ensure that your Python 3 is not set at installation to be the default Python in Terminal (i.e. not on PATH). And if for some reason it does end up in PATH, hunt it down and remove it from your ~/.bashrc, ~/.profile, ~/.bash_profile. I prefer to have no default python and only to set it into PATH as needed using an alias.

Eclipse wont import my enthought modules

I have installed eclipse installed along with enthought. I can import my standard python modules no problem, but as soon as try to import my enthought modules I get an import error. I cant find the folder/location as to where the enthought modules are, so I can add that folder directly to eclipse.
Can someone point me as to where enthought installs the modules so I can add it to eclipses python path?
EDIT:
I'm on Mac OS X 10.7.2
Using Python 2.7
First, you need to install pydev, the Python plugin for Eclipse.
Because only with that the PYTHONPATH becomes manageable.
Then you need to go to Settings->Pydev->Python Interpreter and pick the python interpreter you are using (and that uses the correct path to find all the other EPD modules).
So, if for example you are using the newest Enthought 7.2, then your Python interpreter for that would be in:
/Library/Frameworks/Python.framework/Versions/7.2/bin
In there you pick the python executable, and the Pydev plugin will then analyze all paths so that they will be known, even including automatic completions.

Will installing new version of Python conflict with old versions

I'm a newbie programmer just installing Python 3.2, but I know I also have an older version of Python on my machine. in fact, I think Macbook comes with it installed. Do I have to worry about having different versions on my computer when I try to start learning Python?
For the most part, you don't have to worry about conflicts with system Python. In fact it is recommended to install a different Python version instead of working with system Python. Also consider using virtualenv and virtualenvwrapper to maintain any dependencies for each project easily without conflicts.
It really depends what OS you're talking about. I'm assuming you're talking about a Mac, since you mentioned Macbook.
Macs come with 2.5 and 2.6 installed as far as I'm aware. At least mine has both those versions, and I've only installed 2.7 manually.
You can check which version of python is the current 'system' python by doing the following in terminal:
// check the version of system python
python --version
// tells you where the system version of python is on your PATH
which python
On *nix type Operating Systems, like your Mac, applications aren't really 'installed', like they are in Windows (eliding details). Instead, application files are placed in various different parts of the file system. Python, for example, is placed into the following directory (by default) when installing 2.7:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Since this directory isn't on the system PATH, this version of python won't be used when simply calling python from the command line. The system will search all the folders in the PATH environment variable for an executable file called python. It will usually find it in /usr/bin/ or something similar.
To make a new version of Python the 'system' python, you have a couple of options:
Modify your .bash_profile, and prepend the path to your new python to the PATH environment variable.
symlink the new version of python to a directory already on your PATH like /usr/bin/
Be aware that Mac python installers can modify your .bash_profile (in your home directory), to force the new version to be the default system version. This is what my bash_profile shows:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
You can happily run multiple versions of python on the same system. A particular version is usually the default though, and that's whatever executable python file is found on the PATH first.
If you want to use a different version at any particular point in time, you can:
/path/to/python/2.4/python some_script.py
/path/to/python/2.7/python some_script.py
/path/to/python/3.2/python some_script.py
That will execute the script some_script.py under 3 different versions of python. Of course, you need to make sure that the /path/to/python is correct.
So yes, you need to be mindful about what version of python you are going to be using, hopefully this will guide you into understanding how applications are installed and which version of an application is launched by default when you don't provide a path.
Yes, 3.x Python syntax is not backward-compatible with 2.x. So if you learn Python 3.x you might not be able to port your knowledge to Python 2.x.
Moreover you should choose if you want to learn 3.x or 2.x. 2.x is far more widespread than 3.x, but 3.x is where Python is heading. No more innovation will happen in 2.x, and in mid-term most frameworks will be ported to 3.x (right now there are some notable exceptions)
Hope that helps!
In general, you should be fine. Since the Mac is BSD-based, it should maintain the "python" command as pointing to the version that your system requires, which is usually an older version like 2.5. You may have to use a command like python3 to run your Python 3 programs, but other than that it should be transparent to you.
As you learn and become more advanced, you can begin using the virtualenv system to maintain separate Python installations for multiple projects.
Python version with different major or minor version numbers can be installed in parallel. For example, you can have 2.4, 2.5, 2.6, 2.7 and 3.1 on the same machine. However, you can't have versions with the same major and minor number installed at the same time (at least, not without tricks), so you can't have 2.5.2 and 2.5.4 at the same time.
Note that you will have to install any third-party libraries once for every Python version.
It is very well possible to have multiple versions of python on your machine. Just make sure, that if you call python in your console it uses the python you want it to use. Same goes for your IDE.
Regarding the version: It is always nice to have the latest version on board (in python however there are compatibility issues to take into account) , since there might be features you want to use, that are only available with a certain version and upwards. Since this is sometimes tricky to find out, especially if you are new to the field, going with the latest version might be how you should proceed.
Be careful before installing new version of python.
Python has no backward compatibility.
Scripts written for python 2.7.* won't work on python 3
For example,
print "Hello" will work on python 2.7 but not on version3

Categories

Resources