I have a Pycharm project where I have copied two github projects (download zip and copy paste into project). My problem is that I cannot access the from main, which is located at root.
For some unknown reason from . syntax only shows me files/folders I have created myself.
I'm trying to access build module where there is a class TFNET which I want to import
from darkflow-master.darkflow.net.build import TFNET
Downloading directories like this isn't how you'd typically include external dependencies in a python project.
A more conventional approaches would be ot install the project using pip, it looks like darkflow gives information on how to do this.
Alternatively, just ensure the libraries are in your PYTHONPATH, it looks like pycharm has a way to do this: PyCharm and PYTHONPATH
Reading between the lines, it sounds like you have downloaded two libraries from github, and copied them into your project.
Python needs to know where to find source files.
If you want do it this way, you need to tell your python environment where to find the new sources. Pycharm looks after python environments while you are in python.
Please see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
but this won't tell python outside of Pycharm where to find your library source.
pip is most likely the answer. pip can install globally or inside python virtual environments, in either case, it puts the library code in a location python is expecting to find it.
On this point, please learn about python virtual environments. These are self-contained python mini-worlds. In a venv, you can run a specific version of python with specific packages. Pycharm works well with them, it is easy to set up virtual envs with pycharm. When you are 'inside' a venv, pip will install into the venv, therefore not touching your system python or the python of any other projects.
Also, pip normally installs from an official repository (pypi) but you can tell it to use a git repository as the source of your install. Normally people who write libraries send their mature versions to pypi so it is unusual to fetch from a git repository, but if you want the very latest version, or if the author has not published the library, it's an option.
Note that pip doesn't work with any arbitrary python code. It must be set up to be seen by pip as a python package.
Related
I am using the cloudscraper python library, installed from the Pycharm UI.
Therefore, I am using the main version of this package.
I would like to try to use the dev version of this package, that can be downloaded through github from the relative branch (https://github.com/VeNoMouS/cloudscraper/tree/dev). In order to install this dev package, I have to run python setup.py install.
Is there a way to keep both versions of this module? How can I install the dev package directly from the UI?
Python does not handle having multiple versions of the same library installed. See for example this related question.
Indeed, the solution is to modify the files for one of the version to give it a different name (for example cloudscraper-dev).
Or you could have two different virtual env, one for each version, but it requires to switch from one to the other.
I want to download python libraries like NumPy, scipy, etc. in a separate folder. I want to include that folder in the python project so that whenever I switch to some other laptop, I don't need to install the libraries again rather I import libraries from that folder. Is there any way?
You can easily install python virtualenv.
Your libraries will be installed in directory created by virtualenv.
https://pypi.org/project/virtualenv/.
Other option, you can also use docker.
I suggest using virtual environment in this case. You could use pipenv so that the project hast exactly the libraries you need for it to run.
You can do it.
for numpy library: https://pypi.org/project/numpy/#files
You can download the files statically from pypi.
I would not recommend you go with this approach. There are several reasons to do that.
There would be a dependency on this kind of library. So you have to keep these dependencies along with the NumPy package.
These libraries are getting updated after some time with some newly added functionality and some bug fixes. So with the time other libraries might not compatible with this library.
Recommended way:
Just create an requirement.txt file that contains all the dependency with its version number.
whenever you want to use your project elsewhere, just install all these libraries with below command.
pip install -r requirement.txt
There are two major way you can install python libs to a separate folder: a virtual environment or a container.
Virtula environment (like venv, pipenv, etc) is good as this is the simplest way to your project's own liblaries set which is not impact any other pythonic script in your system. The downside of this case is thet you really have to set up an environment (including lib install) on every computer you move your script to. This can and should be autimated, of course, but this should be done either way.
The container, in other hand, requires additional resources to handle and to build, build, but it is exactly the box with a specific version of your script along with all libs and binaries it requires. No need to reinstall libs while moving to new laptop/desktop/server/cloud/whatever. For this case I would recommend the Docker/Kubernetes. But it's better to start with Docker.
I am relatively new to Python so please pardon my ignorance. I want to know answer to following questions
How does pip know the location to install packages that it installs? After a built of trial and error
I suspect that it maybe hardcoded at time of installation.
Are executables like pip.exe what they call frozen binaries? In essence, does it mean that pip.exe will run without python. Again after a bit of trial and error i suspect that it requires a python installation to execute.
P.S: I know about sys.prefix,sys.executable and sys.exec_prefix. If there is anything else on which the questions i asked on depends, pls link me to same.
PIP is a package manager for Python packages, or modules if you like.
pip when used with virtualenv will generally install packages in the path /lib//site-packages.
For example, I created a test virtualenv named test, and the django folder is in test/lib/python3.7/site-packages/django.
At the time of installation, you must have set up environment variables, and that is how pip recognizes directories.
pip.exe which is placed under path\Scripts needs a python installation and can't run without one. It is hardwired against a specific python interpreter, and can't install packages for another one. If you have 7 different python versions installed on your system, you will also have 7 different versions of pip.
Since it is bound so tightly, pip was at some point even included with the python standard library (see pep-0453 for details).
This also answers the other part of your question of how pip figures out the right location - there is only one location it can install to, the side-packages of the python interpreter it is bundled against.
I used to work with windows when I install a non basic python package with the cmd, but I dont know how to do it on mac, this packaged cant use pip to install on the terminal
It really depends a lot on the specific package. The options can be different, let me present you some:
You can simply add the source files (if they are not properly packaged) to your project and import them
In the same scenario, if the script is not packaged, you can check if there is a way to install it (read the docs). A common way to do it is by having a script in root called setup.py. Try to see if there is a setup script or read the docs to see if there is a way to install it
Check if the package can not be installed with another general package management (you mentioned you use a mac so check with homebrew)
Another possible package management tool for Python is Anaconda, check that one too
Hope this helps!
I'm trying to figure out how I can install a python package that doesn't have a setup.py file with pip. (package in question is http://code.google.com/p/django-google-analytics/)
Normally I would just checkout the code from the repo and symlink into my site-packages, but I'm trying to get my whole environment frozen into a pip requirements file for easy deployment and testing.
Any ideas?
Fork the repo and add a working setup.py. Then send a pull request to the author.
Oh, it's on Google Code. Well then, file a bug and post a patch.
If the author refuses to make their code into an installable Python distribution (never happened to me), just host your fork somewhere and put that in your requirements file.
You can't. PIP installs Python packages. That's not a Python package. I've heard that the Django community in general doesn't make much packages, which makes things like what you are trying to do tricky. But that could be wrong.
If you want to freeze your environment you might want to look into Buildout. Other options in this case is to use an svn:external.