Python Code works in PyCharm on Mac but not on FreeBSD Jail - python

I have seen here, here and here but still not sure how to fix it.
I am doing my dev work on my Mac using PyCharm and everything works and imports.
When I take this project directory to FreeBSD jail. It fails to load internal directory code. and gives the following error:
Traceback (most recent call last):
File "project/src/my_script.py", line 3, in <module>
from src.auth import MyClient
ModuleNotFoundError: No module named 'src'
I am trying to execute the file my_script.py which imports src.auth.py
My Directory structure looks like this:
project
/src
__init__.py
my_script.py
auth.py
__init__.py
.env
both __init.py__ are empty files. Any help would be appreciated.
thanks

Related

Do I have a problem with __init__.py files?

__init__.py files are meant to initialize packages and/or to set what directory is a package, as far as I know.
So my question is, even though these are implicitly run when a package/module is imported, is it normal to receive errors when you run that __init__.py file directly? and why?
I was testing a project I'm currently working on, which is working great, no errors when I import from root files, but what got my attention is that when I execute every module just to check for errors, all the __init__.py files are the ones that throws errors.
For the sake of education,
I followed these 'tutorials' and ran every __init__.py files of every package (directly, not importing a module/package) just to identify if I'm doing something wrong and received errors on those too.
Please, take a look at these examples:
Article: Understanding Python imports, __init__.py and pythonpath — once and for all
Traceback (most recent call last):
File "c:\Users\username\Desktop\python_packages_tutorial\utils\__init__.py", line 1, in <module>
from utils.lower import to_lower
ModuleNotFoundError: No module named 'utils'
I even added the directory to PYTHONPATH env variable, and sys.path even recognizes the directory where it is located. As I said before, it's just happening when I directly execute the init.py file, the rest is working well.
Article: Introduction to Create Own Python Packages
Traceback (most recent call last):
File "c:\Users\username\Desktop\medium_python_packages\my_package\__init__.py", line 1, in <module>
from .my_module1 import *
^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: attempted relative import with no known parent package
My project __init__.py file:
sys.path
[
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\register',
'C:\\Users\\username\\Desktop\\code\\projects\\flask_app\\register', <-- PYTHONPATH env
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311',
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\flask',
'c:\\Users\\username\\Desktop\\code\\projects\\flask_app\\flask\\Lib\\site-packages'
]
Traceback
Traceback (most recent call last):
File "c:\Users\username\Desktop\code\projects\flask_app\register\__init__.py", line 10, in <module>
from register.routes import *
ModuleNotFoundError: No module named 'register'
But they are working as expected.

VS Code recognized the import but still shows ModuleNotFoundError

I am currently developing some packages with multiple subfolders. However, when I try to import one function from a subfolder into another one, the VS code shows green but when I run it, the terminal says ModuleNotFoundError. The folder structure is shown below.
Folder
|-Subfolder_1
|-- __init__.py
|-- fitting.py
|- Subfolder_2
|-- __init__.py
|-- process.py
|- main.py
When I import subfolder_1 to main.py, it works fine, but when I try to import fitting.py to process.py, the problem occurs. I tried to mess around with the settings in vscode but nothing worked. I also tried to just put them in the same subfolder, but when I do that, the process.py functions properly but main.py throws an error for modules not found since I use process.py in the main.py.
If anyone has some idea of how to fix it, it would be greatly appreciated.
EDIT 1:
Tried the sys.path.append for importing and the same error occurred.
Traceback (most recent call last):
File "path/to/file/subfolder_2/process.py", line 6, in <module>
import fitting
ModuleNotFoundError: No module named 'fitting'
EDIT 2: When I move fitting.py into the same subfolder with process.py and import fitting, the code runs and behave properly (folder structure after the traceback message). However, when I call functions in process.py in main.py by import, this error occurred:
Traceback (most recent call last):
File "/path/to/main.py", line 4, in <module>
from subfolder_1 import process
File "/path/to/subfolder_1/process.py", line 4, in <module>
import Fitting
ModuleNotFoundError: No module named 'Fitting'
Folder
|- Subfolder_1
|-- __init__.py
|-- process.py
|-- fitting.py
|- main.py
EDIT 3: I am currently working with a workaround for this by having fitting.py and process.py in the same subfolder but not calling process.py within main.py and have another file for the functions used for main.py; however, I would like to know why is this error occurring and what can fit it since it would be great for future reference if I have to revert back to the old structure.
If you are using virtual environment then you need activate it.
for Mac OS / Linux
source <path to env>/bin/activate
for Windows
<path to env>\Scripts\activate
You can try adding a path. However, it should be noted that vscode searches for files with the workspace as the root directory.
Here I assume that you have a workspace that contains your directory structure.
You can try:
import sys
sys.path.append(".\Folder\Subfolder_1")
import fitting
Example:
Here my_python is my workspace. And it involves a project named mainfloder like your **Floder**.
import sys
sys.path.append(".\mainfloder\projectfloder\pages")
from webpages import SignUpPage
print(SignUpPage())
I can get it.

ModuleNotFoundError on different OS with same setup for local package

Essentially, the main issue is that a package is not being seen for importing on Ubuntu where it is for Git Bash on Windows.
Here is the relevant directory structure with the library folder being the problematic package/module.
project-dir/
services/
task.py
library/
__init__.py
module.py
In the task.py file, I have imports that take the following syntax:
from library.module import function
In the project-dir folder, I run the following command: python services/task.py.
On Git Bash on Windows, this works perfectly. However, on Ubuntu, I get a ModuleNotFoundError thrown. Below is the abstracted aforementioned error:
Traceback (most recent call last):
File "services/task.py", line 3, in <module>
from library.module function
ModuleNotFoundError: No module named 'library'
Note: I saw this question, which looks very similar to my question, but adding things to PYTHONPATH did not fix things. Here is the output for PYTHONPATH for me:
/home/username/.local/lib/python3.6/site-packages:/usr/lib/python3.6:/usr/lib/python3.6/lib-dynload:/usr/local/lib/python3.6/dist-packages:/usr/lib/python3/dist-packages
Since no one posted an answer, and #forty_two happened to give me insight into what I need to do, I will explain my implementation of his suggestion.
I created a file called add_project_path.py in the services directory. The scripts contents is as follows:
import sys
import os
# Adds the project path
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
This gets the add_project_path.py's path, gets the directory in which it resides, and gets that directory's parent directory, which is the project folder. Then, the project folder is added to the path, which solves the issue.
Edit:
Also, to explain further, I added import add_project_path at the top of my imports for task.py, which allowed the add_project_path module to import the project path before any of the other imports occur.

Python module not found but exists in folder

I'm trying to execute a simple PY file and I'm getting the following error:
Traceback (most recent call last):
File "docker_pull.py", line 8, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Looking at Python install folder I found requests module under: C:\Program Files (x86)\Python 3.7.1\Lib\site-packages\pip\_vendor\requests. How can I force Python to use the module already installed ?
P.S: I don't have internet connection in this machine.
This is a very typical issue, so I will leave this here for future reference.
If I have a project called: my_project
.
└── my_project
├── first_folder
├── second_folder
└── third_folder
What you want to do is one of these two things:
PYTHONPATH (only Python)
cd ~/my_project && export PYTHONPATH=$(pwd)
change dir to root dir, and export that dir to PYTHONPATH, so when Python runs it looks in the PYTHONPATH.
PATH (everything)
cd ~/my_project && export PATH=$PATH:$(pwd)
Same as above!
This needed to live somewhere since it took me a while to figure out. So for anyone in the future who needs help with this!
Check the python version you are calling. Run "python --version". If it's not 3.7.1, that's the problem. If it returns a version beginning with "2", then try running the python3 binary instead.

How do I get unittest run in pycharm when import something from __init__.py

I'm trying to run tests in my project in pycharm with the following structure:
root/
tests/
__init__.py
mytest.py
When I import my_settings from init.py into mytest.py, I got the following error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line 116, in <module> modules = [loadSource(a[0])]
File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line 40, in loadSource module = imp.load_source(moduleName, fileName)
File "/Users/user/temp/test/tests/mytest.py", line 2, in <module> from tests import my_settings
ImportError: cannot import name my_settings
I can run test fine with the following command:
python -m unittest discover -s tests/
Here is a sample project: https://github.com/xcompass/pycharm-broken-test-path
I'm running pycharm 3.4.
This is far too late for the original poster, but I had exactly the same problem with PyCharm and thought putting my solution might help those that stumble across this via search.
One possible explanation for this problem, is that Python is not importing from the path you expect. Insert the following lines above the point where the ImportError occurs:
# using example module from above
import tests
print(tests.__file__)
Now re-run the tests in pycharm and examine the output.
What you may find, is that the path to the module "tests" is not what you expect. If that is the case, removing the errant module should fix it for you.
If that doesn't help, check the python path is what you expect by inserting the following lines above the error instead:
import sys
print(sys.path)
Also remove any build directories in your path, so you don't accidentally import a bad egg.
Finally, remove any *.pyc files from the directory:
find -name '*.pyc' -delete
This last one probably won't impact the problem above but caused py.test to fail for me after rebuilding a clean vagrant box.

Categories