I had trying a app using Flask.
I'm used my package UsefulDeveloperTools.
Its __init__.pylike this: (__init__.py)
"""
Useful Tools.
"""
import threads
import IDgenerator
And its directory is like this: (directories
.
\ __init__.py
\ threads.py
\ IDgenerator.py
And I pushed it in TestPyPI and installed it in my virtual environment used python3 -m pip install --upgrade --index-url https://test.pypi.org/simple/ --no-deps UsefulDeveloperTools.
And I have activated my environment,
and run following code use python3 main.py: (main.py)
import flask
from threading import Thread
import time
import random
import UsefulDeveloperTools # Error this
import logging
app=flask.Flask(__name__)
# unimportance
But python raised an error: (terminal)
(Blog) phao#phao-virtual-machine:~/桌面/pypoj/Blog$ python3 main.py
Traceback (most recent call last):
File "/home/phao/桌面/pypoj/Blog/main.py", line 5, in <module>
import UsefulDeveloperTools
File "/home/phao/桌面/pypoj/Blog/lib/python3.10/site-packages/UsefulDeveloperTools/__init__.py", line 5, in <module>
import threads
ModuleNotFoundError: No module named 'threads'
Why? What wronged? How can I finish it?
P.S. This is my packages in my environment: (terminal)
(Blog) phao#phao-virtual-machine:~/桌面/pypoj/Blog$ pip list
Package Version
-------------------- -------
click 8.1.3
Flask 2.2.2
itsdangerous 2.1.2
Jinja2 3.1.2
Markdown 3.4.1
MarkupSafe 2.1.1
pip 22.3.1
setuptools 59.6.0
UsefulDeveloperTools 0.2.2
Werkzeug 2.2.2
I looked on your repo and you are not following the packaging guide at all. You should reorganize your code in a proper file structure and set up eg. a pyproject.toml.
Python has an easy tutorial regarding packaging you can find here:
https://packaging.python.org/en/latest/tutorials/packaging-projects.html
Additionally your __init__.py is wrong. There are several options how you can put imports in your init file. The closes to what you attemptet would be to place a dot before the module names. But I don't know if that helps, before fixing the general package.
"""
Useful Tools.
"""
import .threads
import .IDgenerator
Check out this article for different options for init styles:
https://towardsdatascience.com/whats-init-for-me-d70a312da583
if you have made the module then keep it in the same directory as your file and run it.
Related
I have just uploaded a Python package into pip called hqc:
https://pypi.org/project/hqc/0.0.1/
then successfully pip installed the package using:
pip install hqc
but I can't seem to import the package in Python Interpreter. When I do this, I get the following error message:
>>> import hqc
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import hqc
ModuleNotFoundError: No module named 'hqc'
What am I missing? Do I need to set the environment variable PATH or is it something else?
In the folder where setup.py is, make a folder called hqc and put an empty file called __init__.py (thats two underscores on each side). Right now, there's no file to import so python fails when importing it.
hqc-0.0.1:
hqc:
You can test the package by going to the directory where setup.py is and running
pip install .
Before trying again, be sure to uninstall
pip uninstall hqc
I had a quick look at your github repository and found that the package name, i.e. your folder name is skltemplate.
So, your import will be: (verified it to be working)
import skltemplate
And found that skltemplate package exposes the following modeules:
TemplateClassifier, TemplateTransformer and TemplateEstimator.
You can import the above by doing:
from skltemplate import TemplateClassifier
Now, if you want your package to be named as hqc, you need to rename skltemplate directory to hqc AND run the setup.py again. (You can refer this stackoverflow answer for more details).
Ensure that you increment the package version inside setup.py before running it.
My project structure looks like the following:
my-project:
dialog
utils
path.py
After install by 'pip install .', in PyCharm, I can import successfully either of the two:
from dialog.utils import path
import dialog.utils
Then I can refer to it as below:
path.module_path() // OR
dialog.utils.module_path()
It doesn't run into any problem. I can also do this in iPython's terminal. However, when I run a python through terminal:
python3 run_dialog
run_dialog.py import 'path' as shown above, it always reports this problem:
Traceback (most recent call last):
File "run_dialog.py", line 15, in <module>
import dialog.utils
ModuleNotFoundError: No module named 'dialog.utils'
So basically, I can run this code in both PyCharm and iPython terminal, but not in the project with 'python3 run_dialog'.
Why is that?
Try this
import sys
sys.path.append('my/path/to/myModule/dir')
import myModule
The Python interpreter needs to know to path to your imported module.
However, a better approach would be setting PYTHONPATH to your project directory like this
set PYTHONPATH=my/path/to/project
In my humble opinion, the Pythonic solution would be to install your package in the activated virtual environment using an "editable" install.
“Editable” installs are fundamentally “setuptools develop mode” installs.
use the pip install -e <path> option to do an editable install
(venv) $ pip install -e path/to/myproject # wherever myproject/setup.py is located
you can use pip list to confirm your package is installed into the virtualenv as an editable package, ie the path to the package is listed
$ pip list
myproject 0.1 path/to/myproject
pip 19.1.1
setuptools 41.0.1
changes saved to your package take effect each time you restart the interpreter and reload/import the package, letting you "edit" or "develop" your package
I'm using Heroku as a development server. When I try to push my Django application to Heroku it first tries to install my packages from the requirements.txt file.
requests==2.18.3
ssh-import-id==5.5
The problem is I have a dependency on one of my packages with others. In the above packages, ssh-import-id needs requests package already installed. So when I push the app, pip fails to install and stops the deployment.
Collecting requests==2.18.3 (from -r re.txt (line 1))
Using cached https://files.pythonhosted.org/packages/ba/92/c35ed010e8f96781f08dfa6d9a6a19445a175a9304aceedece77cd48b68f/requests-2.18.3-py2.py3-none-any.whl
Collecting ssh-import-id==5.5 (from -r re.txt (line 2))
Using cached https://files.pythonhosted.org/packages/66/cc/0a8662a2d2a781db546944f3820b9a3a1a664a47c000577b7fb4db2dfbf8/ssh-import-id-5.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-go0a5mxf/ssh-import-id/setup.py", line 20, in <module>
from ssh_import_id import __version__
File "/tmp/pip-install-go0a5mxf/ssh-import-id/ssh_import_id/__init__.py", line 25, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-go0a5mxf/ssh-import-id/
I need to install all the listed packages using pip in single attempt. Because by default Heroku runs, pip install -r requirements.txt.
This is a bug.
The library's setup.py imports the library to get the version for inclusion in the setup() function call...
import os
from setuptools import setup
from ssh_import_id import __version__
... and the library tries to import requests which doesn't yet exist in the environment. This is ssh_import_id.__init__.py:
import argparse
import json
import logging
import os
import platform
import requests # <=== here
import stat
import subprocess
import sys
import tempfile
A fix has been added which works around needing to import the package to get the version...
import os
from setuptools import setup
import sys
def read_version():
# shove 'version' into the path so we can import it without going through
# ssh_import_id which has deps that wont be available at setup.py time.
# specifically, from 'ssh_import_id import version'
# will fail due to requests not available.
verdir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "ssh_import_id"))
sys.path.insert(0, verdir)
import version
return version.VERSION
... but the fix isn't in the current pypi version 5.6.
You could install latest master branch from source instead of pypi by changing your requirements.txt to something like:
-e git+https://git.launchpad.net/ssh-import-id#egg=master
Im trying to install and use stats_exporter from opencensus.ext.stackdriver using the following guide: opencensus-ext-stackdriver
after installing it through pip:
pip install opencensus-ext-stackdriver
Im trying to import it and:
from opencensus.ext.stackdriver import stats_exporter as stackdriver
ImportError: cannot import name 'stats_exporter' from 'opencensus.ext.stackdriver'
When comparing the Git repo, and my local venv/lib/python3.7/site-packages/... it seems like the pip version isn't compatible with Github , so i tried to install it though cloning, and using setup.py
pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
which gives me the following error:
(venv) Yehoshaphats-MacBook-Pro:present-value yehoshaphatschellekens$ pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
Processing /Users/yehoshaphatschellekens/opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/setup.py", line 17, in <module>
from version import __version__
ModuleNotFoundError: No module named 'version'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/
Similar errors of this type indicated that i need to upgrade setuptools, tried that also :(
This post suggests that it might related to the fact that i'm using python3, which isn't completable with version though i really need to install this package on my python3 venv.
Any Help on this issue would be great!
Try this:
#!/usr/bin/env python
import os
from opencensus.common.transports.async_ import AsyncTransport
from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter
from opencensus.trace import tracer as tracer_module
from opencensus.stats import stats as stats_module
def main():
sde = stackdriver_exporter.StackdriverExporter(
project_id=os.environ.get("PROJECT_ID"),
transport=AsyncTransport)
tracer = tracer_module.Tracer(exporter=sde)
with tracer.span(name='doingWork') as span:
for i in range(10):
continue
if __name__ == "__main__":
main()
and
grpcio==1.19.0
opencensus==0.3.1
opencensus-ext-stackdriver==0.1.1
NB The OpenCensus libraries need gRPC too.
You will need:
a GCP Project and it's Project ID (${PROJECT_ID})
a service account with roles/cloudtrace.agent and its key.
Then:
virtualenv venv
source venv/bin/activate
export PROJECT_ID=[[YOUR-PROJECT-ID]]
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json
pip3 install --requirement requirements.txt
python3 stackdriver.py
I am trying to run a script that was given to me and when i try to run it, I am given the following error message:
Traceback (most recent call last):
File "C:\Users\XX\Documents\XX\Project\Experiments\Y-Maze\Data\M06\ymaze_working_script1.py", line 74, in <module>
from playback import Playback
ImportError: No module named playback
Code that this error is from:
#################################################################
### DON'T EDIT FROM HERE ###
from ymaze_track import MouseTracker
from ymaze_mark import Marker
from ymaze_track import FileHandler
import os
import csv
import numpy as np
import time
from tkFileDialog import askopenfilenames
import sys
import Tkinter as tk
from playback import Playback
Is playback a module that needs to be added to python or is inbuilt in? and if so - where can I find it?
playback is not a standard python module so you must add it to your own system python modules
You can do this with run pip install playback
If you got other errors same as the error you described you can find package installation command by
searching in https://pypi.org/
And if your python code has requirement text file which most of the times named
requirements.txt
You can run pip install -r requirements.txt
For installing all needed package easily
playback is not a standard python module, so you have to install it to use it. Run this command from the command line:
pip install playback
If that doesn't work, this should:
py -m pip install playback
Here is a tutorial on using pip to install python modules.