I know proper python importing has been asked time and time again, but I'm not sure how to taclke this situation.
I'm developing a few scripts that depend on each other to work. I am also making a streamlit app to launch the main scipts. This is the folder structure:
gui.py
pages/page1.py
pages/page2.py
database/mongo_queries.py
database/mongo_methods.py
database/mongo_objects.py
mongo_queries imports methods and objects straight up, as they are in the same folder.
import mongo_methods as mm
I'm running streamlit run from the root folder (this may get migrated to a docker
later). gui.py and all pages require importing from ./ using database.mongo_methods.
import database.mongo_queries as mq
So I'm getting the following import erorrs when trying to use one of the functions from the script
ModuleNotFoundError: No module named 'mongo_objects'
File "pages/1_database.py", line 3, in <module>
import database.mongo_queries as mq
File "{full_path}/database/mongo_queries.py", line 1, in <module>
from database import mongo_methods
File "{full_path}/database/mongo_methods.py", line 1, in <module>
from mongo_objects import Something
Both the gui and the scripts are constantly being updated so I can't keep repackaging and reinstalling the code. What's the proper way of solving this?
Related
So I have a hobby I like to do where I program automated systems that upload things to social media with no human input (and make a little side cash). But anyways, I've been trying to crack YouTube recently. YouTube has their own little way to upload videos automatically, but they watch over it INTENSELY. So, I found a work around using youtube_uploader_selenium, a cool little github project that allows you to upload as many videos as you want to YouTube through selenium firefox.
I know if I can get it to work, I can have a fully automated YouTube channel, however one single, important line of code doesn't work properly.
So the module is accessed via an import command in my main script
from youtube_uploader_selenium import YouTubeUploader
I was able to get that working, however when I did I got an error within the module from this line of code saying that selenium_firefox wasn't a valid pip/module.
from selenium_firefox.firefox import Firefox, By, Keys
So, I did some digging and found a PipInstaller script I could use.
import subprocess
import sys
py_exec = sys.executable
# ensure pip is installed & update
subprocess.call([str(py_exec), "-m", "ensurepip", "--user"])
# install dependencies using pip
subprocess.call([str(py_exec),"-m", "pip", "install", "selenium_firefox"])
That had some odd error involving Rust and the Cryptography pip so I decided to take a different approach. I tracked down the selenium_firefox github then downloaded the module from there. In the script I added a line which made it look for the module selenium_firefox in a certain folder where it was downloaded.
from typing import DefaultDict, Optional
from selenium_firefox.firefox import Firefox, By, Keys # problem code
from collections import defaultdict
import json
import time
from .Constant import *
from pathlib import Path
import logging
import platform
selenium_firefox.path.append(
"C:\\Program Files\\Blender Foundation\\Blender2.92\\2.92\\python\\PythonModules\\modules\\")
That <kinda???> worked. This is the error I currently have,
Traceback (most recent call last):
File "C:\Users\CLASSIFIED\Downloads\ASAS\zxxxs.blend\CLASSIFIEDSCRIPTNAME", line 5, in <module>
File "C:\Program Files\Blender Foundation\Blender 2.92\2.92\python\PythonModules\modules\youtube_uploader_selenium\__init__.py", line 2, in <module>
from selenium_firefox.firefox import Firefox, By, Keys
File "C:\Program Files\Blender Foundation\Blender 2.92\2.92\python\PythonModules\modules\selenium_firefox\__init__.py", line 1, in <module>
from .firefox import Firefox
ModuleNotFoundError: No module named 'selenium_firefox.firefox'
Error: Python script failed, check the message in the system console
I don't know where to go from here. The module within the module within the module has errored. This is what the script for the script that's currently producing this error.
from .firefox import Firefox
from .models import *
from .firefox_addons import *
from selenium_browser import *
I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.
I am working on a project on a Raspberry Pi where I need to create a security camera system. I have 3 python files for this project one for live streaming, another one for motion detection and finally the last for activating the motion detection file from an android application. I am trying to make the live stream and motion detection file use the same camera. In the live stream file(live.py) I have imported a socketserver module to make the live stream, which works well when run. However, when I am trying to import the live.py file into the motion detection file(motion.py), I am getting an Import Error. The error is shown below:
Traceback (most recent call last):
File "motion.py", line 10, in <module>
import live.py
File "/home/pi/codes/live.py", line 7, in <module>
import socketserver
ImportError:No module named socketserver
I am very confused, I don't know what this error is. Any help would be very appreciated. Thank you.
it looks like live.py is in a subdirectory. Python cannot access python files that are NOT in python packages. So, you need to ensure that all the folders that live.py is in are PYTHON PACKAGES.
To make a folder into a python package simply add a init.py file to each folder (you do not need to write any code in this file)
Then when you import live.py the import will look like this
from root.folder_name import live
I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.