简体   繁体   中英

Python and modules have different paths

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 eg 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 .

  1. First, I uninstalled all python programme from my system
  2. During the reinstall, I checked the box to allow adjust the system PATH environment variable
  3. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM