Python and modules have different paths - python

I am learning Python specifically for data science and have little programming language although I'm a bit more familiar with R.
I installed some modules e.g. pandas and matplotlib, using the code:
python -m pip install [name_of_package]
which appear to be successfully installed, however, the console cannot find the packages when I type: import pandas
What I discovered was that the packages and Python have different paths.
For Python, the path is:
C:\Users\stone\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9
For the packages, the path is:
c:\users\stone\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
I believe the different paths are responsible for Python not finding the packages.
My question, therefore, is how can I point pip to install in the Python path? In other words for the import to be successful.
This entry seems to be similar to my question but the answers are confusing and may have been become out of date. If answering, please be explicit with the code. I'm very new to this.
Note: I really don't want to install it in a virtual environment because it is needed presently.
Thank you in advance.

Python is probably not finding your pandas library, because it's installed in a folder where python is not looking into by default.
When you type "import pandas" python is going to look for a package named pandas within a list of folders set by default.
To solve your issue you can move/reinstall pandas into a folder which python searches through by default or you can add another folder to search through.
Here's a way to do it with python adding directory to sys.path /PYTHONPATH

I am the original poser of the question and found a simple solution that worked for me based on the recommendation of python.org.
First, I uninstalled all python programme from my system
During the reinstall, I checked the box to allow adjust the system PATH environment variable
After installing, I used the `python -m pip install
[name_of_package] to install the pandas packages.
These solved the problem and I am now able to import packages without an error message.
The current path to packages is now:
c:\users\stone\appdata\roaming\python\python39\site-packages

Related

VS code display ModuleNotFoundError: No module named 'pandas'

I use VSCode to write python scripts about gateway and created a project. This project needs to use pandas to generate and read Excel.Now, I have installed pandas with pip.I have simply written a demo to test to generate and read Excel, but when the code is integrated, run the code and display ModuleNotFoundError: No module named 'pandas'
I use pip list to display module, when i run the project,Only a part of modules are displayed after pip, and there is no pandas, but when I run a demo, he displays many modules, including pandas. how to solved it?
enter image description here
enter image description here
First picture: pip 21.1.1
Second picture: pip 19.0.3
Obviously, your project and the demo running in different environments.
From the picture you provided, it seems like the project using the global python environment.
You can try to install the packages in the global environment. But it's recommended to create a virtual environment instead.
If you not familiar with the virtual environment you can refer to here
I would uninstall pandas with pip and download anaconda which contains pandas, numpy, and lots of other very useful data science tools. Just open vs code, select the conda python interpreter, and everything should run smoothly. Conda ensures all your data science software works together and gets continual updates. It will download a separate version of python so make note of that in the PATH variables if you want your computer to use conda as the main python interpreter.
I think there are already many posts related to this problem. I think you should go through all that. I am leaving the link of a relatable post here. You should also check if you have selected correct python interpreter.
If you have already gone through all that, let us know.
VS Code: ModuleNotFoundError: No module named 'pandas'
You could be in a virtual environment in the project. Check which python interpreter is in use.
Once you have the right python environment selected, just for good measure, install pandas once again, possibly from the vs code terminal.
pip install
Sometimes when there are more than one python installations available incorrect order of python and python\scripts or python\bin can throw off the import statements.
you can run python -m pip install pandas to install pandas in same env from which python is picked.

IDLE and python is different, not able to install modules properly

thanks for reading this. I am using macOS High Sierra. I am not very familiar with terminal or environment variables, but am trying to learn more. From reading other threads and google, it seems like I either have multiple pythons installed, or have pythons running from different paths. However I am not able to find a solution to resolving this, either by re-pathing my IDLE or deleting it entirely.
I do have python, python launcher, and anaconda (not very sure how anaconda works, have it installed a few years back and didn't touch it) installed. I am trying to install pandas (pip install pandas), which tells me that I have it installed, but when I run it on IDLE, it says module not found. Though if i run python3 on terminal and type my code in, it works (so pandas has indeed been installed).
When i run which python on terminal, it returns
/Users/myname/anaconda3/bin/python
(when i enter into this directory from terminal, it shows that in the bin folder, I have python, python.app, python3, python3-config, python3.7, python3.7-config, python3.7m, python3.7m-config)
When i run which idle on terminal, it returns
/usr/bin/idle (im not even sure how to find this directory from the terminal)
When i run import os; print(os.path) on IDLE, it returns module 'posixpath' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py'
Would really appreciate some help to figure out how to ensure that when i install modules from terminal, it would be installed into the same python as the one IDLE is using. Also, I would like to know whether it is possible for me to work on VSCode instead of IDLE. I cant seem to find suitable extensions for data science and its related modules (like statsmodels, pandas etc). Thanks a lot!
First of all, a quick description of anaconda:
Anaconda is meant to help you manage multiple python "environments", each one potentially having its own python version and installed packages (with their own respective versions). This is really useful in cases where you would like multiple python versions for different tasks or when there is some conflict in versions of packages, required by other ones. By default, anaconda creates a "base" environment with a specific python version, IDLE and pip. Also, anaconda provides an improved way (with respect to pip) of installing and managing packages via the command conda install <package-name>.
For the rest, I will be using the word "vanilla" to refer to the python/installation that you manually set up, independent of anaconda.
Explanation of the problem:
Now, the problem arises since you also installed python independently. The details of the problem depend on how exactly you set up both python and anaconda, so I cannot tell you exactly what went wrong. Also, I am not an OSX user, so I have no idea how python is installed and what it downloads/sets alongside.
By your description however, it seems that the "vanilla" python installation did not overwrite neither your anaconda python nor anaconda's pip, but it did install IDLE and set it up to use this new python.
So right now, when you are downloading something via pip, only the python from anaconda is able to see that and not IDLE's python.
Possible solutions:
1. Quick fix:
Just run IDLE via /Users/myname/anaconda3/bin/idle3 every time. This one uses anaconda's python and should be able to see all packages installed via conda install of pip install (*). I get this is tiresome, but you don't have to delete anything. You can also set an "alias" in your ~/.bashrc file to make the command idle specifically linking you there. Let me know with a comment if you would like me to explain how to do that, as this answer will get too long and redundant.
2. Remove conda altogether (not recommended)
You can search google on how to uninstall anaconda along with everything that it has installed. What I do not know at this point is whether your "vanilla" python will become the default, whether you will need to also manually install pip again and whether there is the need to reinstall python in order for everything to work properly.
3. Remove your python "vanilla" installation and only use anaconda
Again, I do not know how python installation works in OSX, but it should be reasonably straightforward to uninstall it. The problem now is that probably you will not have a launcher for IDLE (since I am guessing anaconda doesn't provide one on OSX) but you will be able to use it via the terminal as described in 1..
4. Last resort:
If everything fails, simply uninstall both your vanilla python (which I presume will also uninstall IDLE) and anaconda which will uninstall its own python, pip and idle versions. The relevant documentation should not be difficult to follow. Then, reinstall whichever you want anew.
Finally:
When you solve your problems, any IDE you choose, being VScode (I haven't use that either), pycharm or something else, will probably be able to integrate with your installed python. There is no need to install a new python "bundle" with every IDE.
(*): Since you said that after typing pip install pandas your anaconda's python can import pandas while IDLE cannot, I am implying in my answer that pip is also the one that comes with anaconda. You can make sure this is the case by typing which pip which should point to an anaconda directory, probably /Users/myname/anaconda3/bin/pip
First: This would be a comment if I had enough reputation.
Second: I would just delete python. Everything. And reinstall it.
To repeat and summarized what has been said on various other question answers:
1a. 3rd party packages are installed for a particular python(3).exe binary.
1b. To install multiple packages to multiple binaries, see the option from python -m pip -h.
To find out which python binary is running, execute import sys; print(sys.executable).
3a. For 3rd party package xyz usually installed in some_python/Lib/site-packages, IDLE itself has nothing to do with whether import xyz works. It only matters whether xyz is installed for 'somepython' (see 1a).
3b. To run IDLE with 'somepython', run somepython -m idlelib in a terminal or console.
somepython can be a name recognized by the OS or a path to a python executable.

How to import python packages without pip install

The server that I am using has an older version of python installed, and I am unable to install packages. Is there a workaround to importing python modules? If I can create my own module and append the directory to be able to import the newly created module, then I am assuming that I should be able to do the same for already built packages. I just need to know how I would be able to install what is required for numpy and then import it using a similar method as when creating modules.
I would recommend that you find a machine to install the same version of Python to match your server. Perform the pip install from that stand alone machine. Do some level of testing to make sure that the code with run with that version of Python and everything is happy. Just the other day I found out the hard way that TensorFlow would now work on the latest Python.
The libraries are being installed for me under c:\users\<profilename>\AppData\Local\Programs\Python\Python36\Lib\site-packages So you could then copy that over to the server.
I found this link that is saying the same thing.
https://superuser.com/questions/943980/is-it-possible-to-install-python-packages-without-a-direct-outbound-network-conn
Good Luck with it.

Installing third party modules in python3 - Ubuntu

In short, my question is, how do I install the latest version of scikit-image into my usr/lib/python3/dist-packages so I can actually use it? I think there is a problem with my understanding of how third-party modules are installed. As a newb, I don’t know how to rectify that, hence this post.
I need help to understand how to install packages in python3 up until now I have used pip/pip3/apt-get/synaptic etc and it has worked fine for many packages. However, I have hit several barriers (Skimage, opencv, plantcv in python3). I must emphasise, the problem I am having is using these packages in python3, not 2.7.
For example, I want to use the latest version of scikit-image (0.14) with python3. (http://scikit-image.org/) I have tried using the installation instructions and have not yet successfully managed to install it. I have navigated to my usr/lib/python3/dist-packages and copied scikit-image into this directory (I have all the dependencies installed in here already).
Image of my folder for dist-packages as proof
As you can see, the folder containing skimage is in the directory I want to be installed in, how do I actually install it? Do I have to extract skimage out of the folder into the directory and then run the install command? If I navigate to usr/lib/python3/dist-packages/scikit-image and then run pip install -e . I get an error, stating that I need numpy. If I write a python script using python3 I can clearly see I have it installed (and I have been using it for a long time). So, there must be a problem in how I have this package in my file system. I think a janky workaround would be to copy all the modules into my working directory and Import them that way as if they were modules I have made myself, but this obviously negates the whole point of installing packages.
This has also happened with another package called plantcv. Where I went into the directory usr/lib/python3/dist-packages then cloned the source from git hub and installed as per instructions. When I import plantcv in my python3 script. It Imports fine. But, there is nothing in it, as python cannot see the modules which are inside this folder at usr/lib/python3/dist-packages/plantcv/plantcv.
There is clearly some comprehension here that I am missing, as I have a similar problem for two packages now. Please, Internet. Help me understand what I am missing!
You simply need to copy the folder in /usr/lib/python3/dist-packages/package-name
However, there are certain things that are specific to python packages. The folder named package name should be a valid package. A good indicator of that is it will contain a file "__init__.py". It is very likely that every sub-directory inside this package directory will contain a "__init__.py" file. It depends on whether there are modules inside these sub-directories.
In your code simply import the package like the following.
import package-name
where package-name can be skimage

Can't import library in Python - Ubuntu 12.04

I have been trying for 3 days now with now luck, I really am desperate.
I have installed NumPy, along with matplotlib. I am trying to include matplotlib into my applications, but it does not work. I am using Eclipse with the PyDev plug-in, but whenever I try to import it, I get an error, even though I have added it to the libraries that it needs to import every time.
I am a beginner with Linux and I don't really know how to do stuff of the top of my head. I would like to know if this is related to PYTHONPATH and if so, how can I change it?
Also, when running whereis matplotlib, I get matplotlib: usr/share/matplotlib.
EDIT
Even though I did not manage to solve the problem, nor am I interested any more, I consider this question closed. I have decided to use the free version of PyCharm, as suggested by #FooBarUser.
On my Ubuntu 12.04, modules like these are installed in /usr/local/lib/python2.7/dist-packages. Adding that to PYTHONPATH may help resolve the issue.In my ~/.bashrc I have
export PYTHONPATH=...
export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
export PYTHONPATH=
Edit: if you also have Python 3 installed on your system, numpy might also be in
/usr/lib/pyshared/python2.7/numpy
If the latest version of numpy was built to be backwards compatible with both 2.7 and 3.*, the installer might put it in that directory which is meant for packages which can be shared across multiple python version numbers.

Categories