Using Module for different python version - python

The default version of python (ie, one that opens on typing "python" in command line) is 2.6 on my server. I also have 2.7 installed. How do I import a module, in this case numpy, in python2.7? When I try to import right now, it gives me an error -
ImportError: No module named numpy
Is there any workaround, apart from downloading the package and doing a build install?

I agree with the comment above. You will have to compile it separately. I once tried a hack of importing and modifying the sys.path however I ran into issues with the .so files.

Related

Difference between version pip show and importlib.metadata.version

I am creating a module, henceforth called mymodule, which I distribute using a pyproject.toml. This file contains a version number. I would like to write this version number in the logfile of mymodule. In mymodule I use the following snippet (in __init__.py) to obtain the version:
import importlib.metadata
__version__ = importlib.metadata.version(__package__)
del importlib.metadata
However this version is wrong. This appears to be the highest version which I have ever installed. For reference the command python3 -m pip show mypackage does actually show the correct version after installing the module locally. I struggle to explain this difference. Can anyone think of a cause of this discrepancy?
I also ran importlib.metadata.version(mypackage) which returned the same incorrect version.
The problem was related to left over build artifacts from using setup.py. importlib and pkg_resources will detect these artifacts in a local installation and pip will not. Deleting the mypackage.egg-info directory fixed the issue.

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

Why imports are not found (when they exist)?

I'm getting an "unresolved reference" error for imports, when the imports exist. What might cause this?
The details:
I'm developing in Pycharm, Python 3.5 32 bit virtual environment.
I'm getting import errors with the Quantopian Zipline library.
I've imported Zipline in Pycharm Project Interpreter.
I'm getting import errors using the example code in the Zipline library...
from zipline.api import order, record, symbol
What's going on here?
If you look at the stack frame, you see that it is structured as follows:
buyapple.py imports zipline
zipline imports logbook
logbook imports gevent
gevent fails to load
So the module is there, is found, but in the course of importing zipline, zipline itself imports another module, and that module imports yet another module, and that module fails.
Something is wrong with the gevent installation - to be more precise, the compiled binary is missing functions that the Python part of the module calls. One simple step you can do is try reinstalling it (via pip install -U gevent or equivalent) and see whether that works.

GDAL Import Error after freezing Python 3 script

I am trying to freeze a Python script that contains an import from osgeo but the executable fails with an ImportError: No module named '_gdal'.
I've stripped down my script to just one line:
import osgeo
When running in Python (version 3.3.3) all is well. I've tried freezing with cx_Freeze and py2exe. Both report that there is a missing module: ? _gdal imported from osgeo (among others) but successfully freeze the script. Then the exe fails with the above ImportError.
I've tried importing _gdal in python and it works. I tried manually including the osgeo module in the freezing options but still get the same error.
There is a similar question here:
Importing GDAL with cx_Freeze, Python3.4
but maybe this isn't a cx_freeze issue because it also happens with py2exe (it has experimental support for python 3 now).
Does anybody know how to fix this?
I found a fix. I edited the osgeo\__init__.py file and modified line 13 like this: import osgeo._gdal. This works but only if the module osgeo._gdal is manually included when freezing. #Thomas K Your solution does the same thing I guess.
Note that the same modification must be applied both at osgeo.ogr (line 18) and osgeo.osr (line 18) modules if they are needed (import osgeo._ogr and import osgeo._osr respectively). This means that these must be manually included when freezing as well. My freezing command now looks like this: python cxfreeze.py module1.py --include-modules=osgeo._gdal,osgeo._ogr,osgeo._osr.
Thanks #Thomas K for your help.

How to make downloaded Python libs work on Windows?

I got this simple problem and I can't find the answer anywhere, I'm wasting a lot of time!
I did a Python programm on Linux (which works OK), but when I try to run it on Windows, there are too problems with libs...
I have installed the libs I need (dateutil, lxml, xmlrpclib...) in C:\Python34\Lib\site-packages. But then, they don't work as they do on Linux. For example:
from dateutil.tz import tzlocal
Gives me next error:
File "C:\Python34\lib\site-packages\dateutil\tz.py", line 9, in module
from six import string_types, PY3 ImportError: No module named 'six'
That is, they are not finding the other modules... why???
Have you try this ?
http://www.instructables.com/id/How-to-install-Python-packages-on-Windows-7/
Maybe it can help
It looks like you're using Python 3.4 which comes with pip. pip is a tool for installing packages and any dependencies they might have (like the srting_types module from your error message). I'd suggest learning how to use it because it resolves most of the packaging problems with you needing to moving things around yourself. See an answer from a different question to learn more about pip.
There are some packages that need to be compiled. This can be difficult on Windows 7 if you don't have the proper toolchain set up to compile packages. I'd recommend Christoph Gohlke's wonderful collection of installable packages for Windows. You just need to make sure to grab the right version. Since 3.4 is still relatively new, some packages may not be available, so be warned.

Categories

Resources