Why am i unable to run my project from the command line? - python

I have created a simple app, doesn't matter what is the purpose but i was trying to run it from the command line but it does not work. I am able to run it and everything from the IDE (PyCharm) but not CLI.
I have init files in every folder and the folder structure looks like this:
Project
├───.idea
│ └───inspectionProfiles
├───files
└───src
├───project
│ ├───Variations
│ │ └───__init__.py
│ │ └───A.py
│ │ └───B.py
│ └───__init__.py
│ └───C.py
│ └───D.py
└────tests
└───__init__.py
└───main.py
When i positioned myself in tests and ran:
python main.py the script returned:
File "G:\Projects\ExampleProject\src\tests\main.py", line 1, in <module>
from project import run
ModuleNotFoundError: No module named 'project'

Related

Pycharm configuration difference between running and debugging

I have a folder structure where
project_name
└───folder_level_1
│ └───folder_level_2
│ │ └───folder_level_3
│ │ └─── folder_level_4
│ │ │ └─── ...
| | | main.py
I have marked folder_level_1/2/3 as source roots in Pycharm.
When I try to run main.py in debug - it works, but if I run normally I get an error that
ModuleNotFoundError: No module named folder_level_1.folder_level_2.folder_level_3. In each level I have an init.py as well.
Running it with pipenv environment if that helps.
How should I solve that?

ModuleNotFoundError: No module named 'SLCT'

English is not my mother tongue, so there might be some grammatical errors in my question.
Sorry about that.I git clone a project from github to my VScode. When I wanted to run demo code, a "ModuleNotFoundError" occured. I was confused about this error. Because I checked module and it did exit, I also haven't install same name module before. Here is the project-tree of the project.(Only parts including "SLCT" are given)
source code
└─ logparser_root
├─ benchmark
│ ├─ SLCT_benchmark.py
├─ demo
│ ├─ SLCT_demo.py
├─ logparser
│ ├─ SLCT
│ │ ├─ cslct.c
│ │ ├─ cslct.h
│ │ ├─ README.md
│ │ ├─ SLCT.py
│ │ ├─ __init__.py
│ │ └─ __pycache__
│ │ └─ __init__.cpython-39.pyc
"SLCT_demo.py" import SLCT from logparser.
import sys
import os
# sys.path.append('../')
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# sys.path.append('../logparser//SLCT//SLCT.py')
# print(sys.path)
from logparser import SLCT
When I ran "SLCT_demo.py", this error occured.
Traceback (most recent call last):
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\demo\SLCT_demo.py", line 9, in <module>
from logparser import SLCT
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\logparser\SLCT\__init__.py", line 1, in <module>
from SLCT import *
ModuleNotFoundError: No module named 'SLCT'
Here is "init.py" of "SLCT".from SLCT import *
Thanks for spending time on my question. Have a nice day!
In order to run from SLCT import * inside file x.py, you need to have the following directory structure:
File x.py
Folder SLCT
- File __init__.py
In your case, you are trying to run from SLCT import * inside file __init__.py, which means that you need to have the following directory structure:
File __init__.py
Folder SLCT
- File __init__.py
And in your case, since this whole thing is already inside a folder named SLCT, you're probably caught in your own confusion.
In short, it sounds like you want to simply move the entire contents of file SLCT.py into file __init__.py (although it's hard to say for sure, because you haven't made the rest of your code visible).

Packaging python project with multiple directories

I need some explanation on working with setuptools and find_packages function.
I have a project structure like this:
├── project_dir_1
│ ├── module.py
│ ├── __init__.py
├── my_project
│ ├── cli.py
│ ├── subdir1
│ │ ├── __init__.py
│ │ ├── module.py
│ ├── conf
│ │ ├── module.py
│ │ ├── params
│ │ │ ├── config.yml
│ │ ├── __init__.py
│ ├── subdir2
│ │ ├── module.py
│ ├── __init__.py
│ └── version.py
├── project_dir_2
│ ├── subdir1
│ │ ├── module.py
│ │ ├── __init__.py
│ ├── __init__.py
├── README.md
├── requirements.txt
├── setup.py
└── tests
└── test_main.py
Actually all my code in the my_project dir and I also have two additonal dirs project_dir_1 and project_dir_2 that contains necessary external modules from where I should do imports both in the package code and in another projects code where this package will be installed in venv.
I have setup script like this:
setup(
name='my_project',
version='0.0.1',
description='Python library.',
license='license',
author='me',
author_email='my_email',
entry_points={'console_scripts': ['my_project=my_project.cli:main']},
python_requires='>=3.7',
packages=find_packages(
include=['my_project', 'project_dir_1', 'project_dir_2', 'my_project.*', 'project_dir_1.*', 'project_dir_2.*']
),
install_requires=list(open(join(dirname(__file__), 'requirements.txt')).read().split()),
)
When I activate venv in another project folder and trying to install package from package root folder like python ..\package_root\setup.py install everything seems to works fine during install. And pip list shows all dependencies and my_project 0.0.1. But if I'm trying to import something from my_project using the venv interpreter I got an error: ModuleNotFoundError: No module named 'my_project'. The same result if I'm trying to import something like from project_dir_1 import module which is also necessary. Also when I just run my_project from shell addressing to cli I got an error:
Traceback (most recent call last):
File "/home/developer/another_project/env/bin/my_project", line 11, in <module>
load_entry_point('my_project==0.0.1', 'console_scripts', 'my_project')()
File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
return ep.load()
File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
return self.resolve()
File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'my_project'
So what is the right way to organize this complex project structure and include all necessary code in setup.py to get the setuptools install the package correctly? I need some better understanding of python projects packaging but still don't get an answers for this case from seeking the docs.
find_packages will resolve paths relative to current working directory, so calling it outside of the project root dir will effectively install nothing (check whether you see any sources installed by e.g. running
$ pip show -f my_project
, I bet nothing will be listed). You have to force switching to the project root dir in the setup script, e.g. add a magic line to your setup script:
# setup.py
import os
from setuptools import setup
# old-style for python 2
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
# new style for python 3
from pathlib import Path
os.chdir(Path(__file__).parent.absolute())
setup(...)

Python import module/function from below subdirectory or anywhere

I would like to load in a function/module with my working directory as project main directory but the function file is store below the a subdirectory level so the normal
from function_file import function_name
does not work.
This is what the project directory set up looks like:
└───project_main_directory
│ .gitattributes
│ .gitignore
│ README.txt
│
├───code
│ ├───exploration
│ └───scripts
│ │ script1.py
│ │ script2.py
│ │ script3.py
│ │
│ └───functions
│ │ function1.py
│ │ function2.py
│ └───__init__.py
│
├───data
│ └───example_data
│ data.csv
└───documents
So I tried to import functions via
import code.scripts.function.function1 from function1
and that doesn't work. I know it's because the other subdirectories aren't modules, but I want to ask if there is away around that?
-- EDIT
I'm working from .py file in code/scripts/script1.py but working directory is project_main_directory/
Add an empty file __init__.py to every subdirectories to make them as modules.
.
├── code
│ ├── __init__.py
│ └── scripts
│ ├── __init__.py
│ └── script1.py
└── main.py
Then if you have a function called hello in code/scripts/script1.py you can import that function by:
from code.scripts.script1 import hello
hello("yo")
If your current directory is project_main_directory, you can use:
from code.scripts.functions.function1 import function1
Directory of your script doesn't matter. Only your current directory matters (refer top of the IDE)
To import function/module from another python file, you have to do something like below -
from code.scripts.functions.function1 import function1
Above we are loading function1 from function1.py file which is stored in functions directory which is stored in scripts directory and finally in code directory.
EDIT - so you are saying, you want to load a function from function1.py in script1.py? In that case from .functions.function1 import function should work.

How to run this module from this path?

I have the following file structure:
Project
|
├───cambridge_loader
| |
│ ├───tests
│ │
│ ├───__init__.py
│ │
│ ├───__main__.py
│ │
│ ├───CambridgeLoader.py
I'm in the Project directory on my command line, I created a virtual environment in Visual Stdio and executed the activate.bat script.
I've tried running:
python -m CambridgeLoader
And get the following error:
C:\Users\username\Source\Repos\rapdataload\Frauenfeld\env\Scripts\python.exe: No module named CambridgeLoader
I've tried running it in different paths but it doesn't seem to be working. I'm not sure why it's attaching the Scripts path at the end...

Categories