Linux python lib path

How To Set Python Module Search Path To Find Modules

When you want to import a python module library in your python source code, you need first to make the python module library importable by adding the module package path in the PYTHONPATH system environment variable. You can also add the python module package path to the python module search path at runtime in python source code. This example will show you how to do it.

1. Add Python Module Package Path In System Environment Variable PYTHONPATH.

Suppose your python module is saved in folder /tmp. We will add /tmp folder in the PYTHONPATH environment variable value.

    Open a terminal and go to the user home directory use cd ~ command.

$ ls -al . -rw-r--r--@ 1 zhaosong staff 1176 Apr 30 09:15 .bash_profile .

2. Display Python Library Search Path In Python Source Code.

    Run into python interactive console in a terminal.

$ python3 Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
>>> import sys >>> for path in sys.path: . print(path) . /tmp /Users/zhaosong/anaconda3/lib/python36.zip /Users/zhaosong/anaconda3/lib/python3.6 /Users/zhaosong/anaconda3/lib/python3.6/lib-dynload /Users/zhaosong/.local/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa
  1. Python sys.path.append function can append directory to the end of python library search directory.
>>> sys.path.append('/abc') >>> >>> >>> for line in sys.path: . print(line) . /tmp /Users/zhaosong/anaconda3/lib/python36.zip /Users/zhaosong/anaconda3/lib/python3.6 /Users/zhaosong/anaconda3/lib/python3.6/lib-dynload /Users/zhaosong/.local/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa /abc

4. Append Exist Module Library Directory To Python Library Search Directory.

  1. Python module’s __file__ attribute returns the module file saved directory. You can append that directory to the python library search path as below.
>>> import sys, os # os.__file__ will return the os module directory. >>> sys.path.append(os.__file__) >>> >>> >>> for line in sys.path: . print(line) . /tmp /Users/zhaosong/anaconda3/lib/python36.zip /Users/zhaosong/anaconda3/lib/python3.6 /Users/zhaosong/anaconda3/lib/python3.6/lib-dynload /Users/zhaosong/.local/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages /Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa /abc /Users/zhaosong/anaconda3/lib/python3.6/os.py

5. Question & Answer.

5.1 How can I add another python source directory in the Python search path for a large python project.

  1. My team just handle an old python project from another team, the python project is so large, there are a lot of python source files in the python project. Our development environment is Linux and no IDE, only in the command line. And when I run the python script with the command python abc.py it prompts the error ImportError: no module named com.test_module. And there is a lot of such kind of errors in other python scripts. All the old python project files are saved in a folder like /codebase/old_python_project. And there are a lot of subfolders in the project base folder. How can I make python search all the modules in the project folder and it’s subfolders to fix the import error? Thanks.
  2. You can add your existing python project folder in the PYTHONPATH system environment variable to fix your issue. You can also use the function sys.path.append(‘/codebase/old_python_project’) in your python script source code and then can import the python modules. If you want to add all the project subfolders in the python module search path, you can reverse loop your project folder and when you reach it’s subfolder then you can call the sys.path.append() function to add the subfolder to the python module search path, you can try it.
import os, sys def reverse_add_python_module_search_path(module_dir): files_array = [] files_array = os.listdir(module_dir) for file in files_array: if os.path.isdir(file): sys.path.append(file) reverse_add_python_module_search_path(file)

Источник

Читайте также:  Папка со шрифтами linux

PYTHONPATH on Linux [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

  1. What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder?
  2. Is Python provided by default on Ubuntu, or does it have to be installed explicitly?
  3. Where is the folder in which all modules are (I have a lot folders called python_ )?
  4. If I wish a new module to work when I’m programming (such as pyopengl) where should I go to introduce all the folders I’ve got in the folder downloaded?
  5. Coming back from the PYTHONPATH issue, how do I configure the PYTHONPATH in order to start working on my new module?

3 Answers 3

  1. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:
# make python look in the foo subdirectory of your home directory for # modules and packages export PYTHONPATH=$:$/foo 

If a Python user has Python2.7, Python3.5 and Python3.6 installed in Ubuntu, echo $PYTHONPATH could return :/usr/local/lib/python3.5/dist-packages:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python3.6/dist-packages

  1. PYTHONPATH is an environment variable
  2. Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
  3. /usr/lib/python2.7 on Ubuntu
  4. you shouldn’t install packages manually. Instead, use pip. When a package isn’t in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
  5. if you use pip or setuptools, then you don’t need to set PYTHONPATH explicitly

If you look at the instructions for pyopengl, you’ll see that they are consistent with points 4 and 5.

PYTHONPATH is an environment variable those content is added to the sys.path where Python looks for modules. You can set it to whatever you like.

Читайте также:  Nvidia driver updates linux

However, do not mess with PYTHONPATH . More often than not, you are doing it wrong and it will only bring you trouble in the long run. For example, virtual environments could do strange things…

I would suggest you learned how to package a Python module properly, maybe using this easy setup. If you are especially lazy, you could use cookiecutter to do all the hard work for you.

I suspect the downvotes happens because the question did not address the OP’s question directly. It now does. It is still a terrible idea to mess with PYTHONPATH .

Источник

Where are the python modules stored?

If you import sys then run sys.path() , it shows all the paths for python. /usr/local/lib/python3.x/dist-packages worked for me.

In python 3.9, path is not a function, it’s A list of strings that specifies the search path for modules . docs

11 Answers 11

Usually in /lib/site-packages in your Python folder. (At least, on Windows.)

You can use sys.path to find out what directories are searched for modules.

If you want the location of a specific module, import it and look at it’s __file__ attribute. Works for most of them.

@NoufalIbrahim your answer is worth like the answer itself. TY. you can append it to make it bold for users.

On python command line, first import that module for which you need location.

For example to find out «pygal» location:

import pygal print(pygal.__file__) 
/anaconda3/lib/python3.7/site-packages/pygal/__init__.py 

Sample output of pip show tensorflow :

Name: tensorflow Version: 2.1.1 Summary: TensorFlow is an open source machine learning framework for everyone. Home-page: https://www.tensorflow.org/ Author: Google Inc. Author-email: packages@tensorflow.org License: Apache 2.0 Location: /home/user/.local/lib/python3.6/site-packages Requires: termcolor, six, astor, numpy, grpcio, absl-py, protobuf, tensorflow-estimator, tensorboard, gast, keras-applications, opt-einsum, wheel, keras-preprocessing, google-pasta, scipy, wrapt Required-by: tf-models-official 

The installed location is shown at Location:/home/user/.local/lib/python3.6/site-packages .

Читайте также:  Jnlp linux launch with

Источник

Оцените статью
Adblock
detector