Thank you, all of you. You really helped me to figure out what the question is. Now let me make this clear, and hopefully this case could help others.

The codes I want to run actually are contained in a dictionary called 'src'. and the programmer wrote their code, for example main.py, with following line: ''' import src.utils as utils ''' I made a mistake that I ran the code in the ~/main/src dictionary, so python returned an error "no src module". I was then stupid to find src package.

Actually, one easy way to run the codes is copying all the .py files to the upper level dictionary, in my case ~/main/, and run main.py in ~/main/, because ~/main/src/ contains utils.py, then the code will run smoothly, and no module missing at all.

Answer from Edw. Y on Stack Overflow
🌐
PyPI
pypi.org › project › src
src · PyPI
This package mitigates the risk and safely exits in the case that you mistype easy_install src or pip install src when no src directory exists.
      » pip install src
    
Published: Jul 17, 2014
Version: 0.0.7
Discussions

Using a src directory for a Python package
The tl;dr (and trust me, it's a ... is this: Python implicitly adds the current directory to sys.path, which leads to nasty and hard to debug problems when it comes to packaging your code. Putting your package inside a src directory means that you have to install it the way ... More on reddit.com
🌐 r/learnpython
14
20
August 16, 2022
python 3.x - I am in need to import src library. I have used pip install src. but an error use to appear - Stack Overflow
I have used pip install src. but an error use to appear, I have downloaded the package on my disk but I don't know how to insert it on the notebook to be used. Downloading https://files.pythonhosted.org/packages/9a/2b/a6ccfc80af698319c54f00da05f6c798cf72291938893f8bd3f730c2daf9/src-0.0.7.zip ... More on stackoverflow.com
🌐 stackoverflow.com
pip install src doesn't work
Hi Mitchellrj, I am trying to install src package, but I couldn't make it. Here I described my problem, could you help me a bit? Thank you very much. https://stackoverflow.com/questions/6264059... More on github.com
🌐 github.com
2
June 29, 2020
python - Does the src/ folder in PyPI packaging have a special meaning or is it only a convention? - Stack Overflow
You can read this article blog.ionelmc.ro/2014/05/25/python-packaging . I hope it helps ... There is an interesting blog post about this topic; basically, using src prevents that when running tests from within the project directory, the package folder gets imported instead of the installed package ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 46919714 › installing-src-package-in-python
cmd - Installing src package in python - Stack Overflow
From the src package directory, run C:\Python27\python.exe setup.py install or add python to your path environment variable following the steps here: adding python path on windows and run python setup.py installfrom the src package directory.
🌐
B-List
b-list.org › weblog › 2023 › dec › 15 › python-packaging-src-layout
Python packaging: use the "src" - James Bennett
December 15, 2023 - Instead, adopt the src/ layout or a variation of it, where your module is inside a directory called src/. This way, even locally and even when you forget to use -I, running tests will require that the package successfully build and install, because the module will no longer be top-level and thus no longer implicitly on the import path.
🌐
Reddit
reddit.com › r/learnpython › using a src directory for a python package
r/learnpython on Reddit: Using a src directory for a Python package
August 16, 2022 - The tl;dr (and trust me, it's a long rabbit hole to go down if you want to read the full back and forth in the community) is this: Python implicitly adds the current directory to sys.path, which leads to nasty and hard to debug problems when it comes to packaging your code. Putting your package inside a src directory means that you have to install it the way your users will, making the src directory itself the package just recreates the problem.
🌐
Catb
catb.org › ~esr › src › INSTALL.html
Installing SRC
Either have "python3" on your $PATH and put src somewhere on your $PATH, or call a Python interpreter explicity to run SRC. Python 2.7 or any Python 3.x interpreter for x >= 3 will work. Have RCS or SCCS or both installed on your system.
🌐
Mlcommons
docs.mlcommons.org › cm4mlops › scripts › Python-automation › install-python-src
install-python-src - CM Script Automation Documentation
import cmind r = cmind.access({'action':'run' 'automation':'script', 'tags':'install,src,python,python3,src-python3,src-python' 'out':'con', ... (other input keys for this script) ...
Find elsewhere
🌐
GitHub
github.com › scottsideleau › get-python-src
GitHub - scottsideleau/get-python-src: Easily fetch, build, and install any version of Python from source. · GitHub
./get-python-src.sh <version> <params> ... "static" - Build the source as static library (.a version) "install" - Install the Python build to /opt/python<version> "clean" - Clean the src/ directory NOTE: The <params> can be stacked ...
Author: scottsideleau
🌐
Python Packaging
packaging.python.org › en › latest › discussions › src-layout-vs-flat-layout
src layout vs flat layout - Python Packaging User Guide
The src layout requires installation of the project to be able to run its code, and the flat layout does not.
Top answer
1 of 2
1

Try pip install src==0.0.6

It worked for me

2 of 2
1

I am in need to import src library.

No, you don't. There is a package named src in pypi which has been made by a benevolent soul designed to not work, because that is not what you want to do.


When you're instructed to use pip install src, what it means is that you're supposed to install the directory src under a local repository as a local package. If that command doesn't work, it's because you're running it in the wrong directory, you skipped downloading the source code of the program you're trying to install, or you failed to read some instruction from the mantainer.

Packages crafted this way have well defined setup.py, setup.cfg, pyproject.toml and all modules stuck inside a src folder, which makes it easy to install the whole program as a python package (or module) by simply writing pip install src in the root folder of the program's source code.

If there isn't a src folder correctly configured as a python package or module, pip will default to searching for a package named src on the pypi default repository (pypi.org). Thankfully, one person decided to make a dummy package on that namespace to avoid someone uploading a virus to your environment, making your code not work so you can refrain from doing something you're not supposed to.

So to fix that undesired behaviour you should make sure that the package you're supposed to install is present in your environment. For cloud environments, the current folder where you run pip install src should be the one where you downloaded the source files for the thing you're trying to install.

🌐
GitHub
github.com › mitchellrj › src › issues › 3
pip install src doesn't work · Issue #3 · mitchellrj/src
June 29, 2020 - Hi Mitchellrj, I am trying to install src package, but I couldn't make it. Here I described my problem, could you help me a bit? Thank you very much. https://stackoverflow.com/questions/62640592/how-to-install-src-0-0-7-package
Author: mitchellrj
🌐
Python Packaging
packaging.python.org › tutorials › installing-packages
Installing Packages - Python Packaging User Guide
It’s recommended to write {sys.executable} rather than plain python in order to ensure that commands are run in the Python installation matching the currently running notebook (which may not be the same Python installation that the python command refers to).
🌐
GitHub
github.com › pypa › hatch › discussions › 1051
"src" layout result in a package named "src" · pypa/hatch · Discussion #1051
The problem might be that the package is installed in /home/user/.local/lib/python3.11/site-packages/src/foo instead of /home/user/.local/lib/python3.11/site-packages/foo.
Author: pypa
🌐
YouTube
youtube.com › watch
Adding a SRC Directory: Part #21 Python API Course - YouTube
Enjoy this completely free 19 hour course on developing an API in python using FastAPI. We will build a an api for a social media type app as well as learn t...
Published: May 2, 2022
🌐
Snyk
snyk.io › advisor › python packages › src
src - Python Package Health Analysis | Snyk
July 17, 2014 - Package managers should never punish the user by downloading and running remote code in this case. This package mitigates the risk and safely exits in the case that you mistype easy_install src or pip install src when no src directory exists.
🌐
Py-pkgs
py-pkgs.org › 04-package-structure.html
4. Package structure and distribution — Python Packages
In general, we recommend using the “src” layout over the “non-src” layout (and so does the Python Packaging Authority) because it has several advantages when it comes to developing and distributing installable Python packages.
🌐
PyPI
pypi.org › project › my-src-project
my-src-project · PyPI
A simple Python package. ... Data sourced directly from PyPI's database. ... Data sourced directly from PyPI's database. ... Download the file for your platform. If you're not sure which to choose, learn more about installing packages. ... Filter files by name, interpreter, ABI, and platform. If you're not sure about the file name format, learn more about wheel file names. ... Details for the file my_src_project-0.1.4.tar.gz.
Top answer
1 of 1
20

There is an interesting blog post about this topic; basically, using src prevents that when running tests from within the project directory, the package source folder gets imported instead of the installed package (and tests should always run against installed packages, so that the situation is the same as for a user).

Consider the following example project where the name of the package under development is mypkg. It contains an __init__.py file and another DATA.txt non-code resource:

.
├── mypkg
│   ├── DATA.txt
│   └── __init__.py
├── pyproject.toml
├── setup.cfg
└── test
    └── test_data.py

Here, mypkg/__init__.py accesses the DATA.txt resource and loads its content:

from importlib.resources import read_text
  
data = read_text('mypkg', 'DATA.txt').strip()  # The content is 'foo'.

The script test/test_data.py checks that mypkg.data actually contains 'foo':

import mypkg
  
def test():
    assert mypkg.data == 'foo'

Now, running coverage run -m pytest from within the base directory gives the impression that everything is alright with the project:

$ coverage run -m pytest
[...]
test/test_data.py .                                             [100%]

========================== 1 passed in 0.01s ==========================

However, there's a subtle issue. Running coverage run -m pytest invokes pytest via python -m pytest, i.e. using the -m switch. This has a "side effect", as mentioned in the docs:

[...] As with the -c option, the current directory will be added to the start of sys.path. [...]

This means that when importing mypkg in test/test_data.py, it didn't import the installed version but it imported the package from the source tree in mypkg instead.

Now, let's further assume that we forgot to include the DATA.txt resource in our project specification (after all, there is no MANIFEST.in). So this file is actually not included in the installed version of mypkg (installation e.g. via python -m pip install .). This is revealed by running pytest directly:

$ pytest
[...]
======================= short test summary info =======================
ERROR test/test_data.py - FileNotFoundError: [Errno 2] No such file ...
!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!
========================== 1 error in 0.13s ===========================

Hence, when using coverage the test passed despite the installation of mypkg being broken. The test didn't capture this as it was run against the source tree rather than the installed version. If we had used a src directory to contain the mypkg package, then adding the current working directory via -m would have caused no problems, as there is no package mypkg in the current working directory anymore.

But in the end, using src is not a requirement but more of a convention/best practice. For example requests doesn't use src and they still manage to be a popular and successful project.

🌐
Medium
medium.com › analytics-vidhya › explicit-understanding-of-python-package-building-structuring-4ac7054c0749
Explicit understanding of python package building (structuring) -part 2 | by nipun deelaka | Analytics Vidhya | Medium
November 22, 2021 - package_dir={'': 'src'},py_modules = ["module_1", "module_2"], This is the simplest way to do that when you are using setuptools as the packaging module. ... Actually, packages are a little bit different in structure as well as in behavior from ...
🌐
pyOpenSci
pyopensci.org › python-package-guide › package-structure-code › python-package-structure.html
Python Package Structure & Layout — Python Packaging Guide
This means that if you run your tests in your package’s working directory, using a flat layout, /package/module.py, Python will discover package/module.py file before it discovers the installed package. However, if your package lives in a src/ directory structure src/package, then it won’t be added to the Python path by default.